Posts

Showing posts with the label SQL Server

Select nth Row from Table in SQL Server

DECLARE @RowNumber INT = 9 WITH TableWithRow AS (       SELECT        ( ROW_NUMBER () OVER ( ORDER BY employees . EmployeeID )) as ROW ,        FirstName , LastName                 FROM employees ) SELECT * FROM TableWithRow WHERE ROW = @RowNumber Output: ROW                  FirstName  LastName -------------------- ---------- -------------------- 9                    Ashvin     Padhiyar (1 row(s) affected)  Note: ROW_NUMBER () is an analytic function. It assigns a unique number to each row to which it is applied, in the ordered sequence of rows specified in the order_by_clause ...

How to Edit More than 200 Rows in SQL Server 2008

How to Edit More than 200 Rows in SQL Server 2008 Management Studio Step 1: In SQL Server 2008 Management Studio, go to "Tools" -> "Options" -> "SQL Server Object Explorer" -> "Commands". Step 2: Now in "Table and View Options" section, you can change either: #Value for Edit Top Rows command, to a value greater than or less than 200. #Value for Select Top Rows command, to a value greater than or less than 1000. Step 3: By specifying a value of 0, SQL Server will return all rows. (If you sql tables are really large data volume, you should NOT set these values to 0.) Step 4: Click OK to save your changes.

Variable Value in Top Statement

i want create a Procedure that behave like this: declare @cnt int set @cnt =10 select top @cnt * from sw_user Where sw_user contains 20 records, and i want the variable to be the criteria of Top statement. so i can use this for a solution for this type of problem CREATE PROC Top10 @cnt int AS SET ROWCOUNT @cnt SELECT * FROM sw_user but some cases ROWCOUNT might not be work... so what, Note : This statement however is possible in SQL 2005, just add "()" in your variable like so: declare @cnt int set @cnt =10 select top (@cnt) * from sw_user

Parsing the comma separated values into a temporary table

(Works in both SQL Server 7.0 and 2000) Declare @OrderList varchar (500) set @OrderList = 'Order1,Order2,,Order3,,,Order4,Order5,' ; drop TABLE #TempList CREATE TABLE #TempList ( OrderID varchar (20) ) DECLARE @OrderID varchar (10), @Pos int SET @OrderList = LTRIM(RTRIM(@OrderList))+ ',' SET @Pos = CHARINDEX( ',' , @OrderList, 1) IF REPLACE(@OrderList, ',' , '' ) <> '' BEGIN WHILE @Pos > 0 BEGIN SET @OrderID = LTRIM(RTRIM( LEFT (@OrderList, @Pos - 1))) IF @OrderID <> '' BEGIN INSERT INTO #TempList (OrderID) VALUES (@OrderID) END SET @OrderList = RIGHT (@OrderList, LEN(@OrderList) - @Pos) SET @Pos = CHARINDEX( ',' , @OrderList, 1) END END SELECT * FROM #TempList t +-+-+-+-+-+ OUTPUT +-+-+-+-+-+ OrderID -------- -- Order1 Order2 Order3 Order4 Order5