I have declared the following cursor and have used a local variable #RowNo to print the number of each each row.
declare TheCursor cursor
for select productName from products
declare #RowNo int
declare #productName nvarchar(50)
set #RowNo = 1
open TheCursor
fetch next from TheCursor into #productName
print #RowNo
print #productName
set #RowNo = #RowNo+1
set #productName=''
while ##FETCH_STATUS=0
begin
fetch next from TheCursor into #productName
print #RowNo
print #productName
set #RowNo = #RowNo+1
set #productName=''
end
close TheCursor
deallocate TheCursor
I am trying to find any other way to assign / define a number to each row and display it in the console. I have found the function Row_number() and used it like select ROW_NUMBER() over (order by (select 0)) As Rownumber to do it.
I want to know if it is possible to use the KEYSET in my cursor to do it? How can I do it with KEYSET?
I don't completely understand what you're trying to achieve - but you could wrap your select statement including the ROW_NUMBER() function into a CTE (common table expression) and then go from there - no messy cursor needed, that really doesn't scale well:
WITH YourSelection AS
(
SELECT
ProductName,
ROW_NUMBER() OVER(ORDER BY ProductName) AS 'RowNum'
FROM dbo.Products
)
SELECT ProductName, RowNum
FROM YourSelection
That should give you the product names, sorted by ProductName, and corresponding row numbers, too.
Related
I am new to programming so I do not know how this question to ask.
what I am doing is firstly finding #userid value on different query, then after when I do
select a,b
from tableA
where userid = #userid
and active=1
and payments=1
then it executes to show me number of rows(lets say:10 rows)
but when I do like below, I only get 1 row (I want to get all 10 rows):
declare #A varchar(10)
declare #B bigint
select #A=a,#B=b
from tableA
where userid=#userid
and active=1
and payments=1
Select #A,#B
so, I am asking for help how do I do this . I have to do like step 2 because i have to run other query taking,#A and #B
Yes because those are scalar variable which can hold only 1 item and in your case it will hold the values for last row. You might want to consider using a table variable rather. Like
DECLARE #tab1 table(
A varchar(10),
B bigint );
Then fill it like
insert into #tab1(A,B)
select a, b
from tableA
where userid=#userid
and active=1
and payments=1
Now select from it
select * from #tab1;
It seems like you are trying to do something with all a and b values of tableA.
You can use cursor for that if number of records are small else use the WHILE loop to read each record.
Declare #a varchar(10)
Declare #b varchar(10)
DECLARE MyCursor CURSOR FOR
select a, b
from tableA
where userid=#userid
and active=1
and payments=1
OPEN MyCursor
FETCH NEXT FROM MyCursor INTO #a, #b
WHILE ##FETCH_STATUS = 0
BEGIN
Select #a, #b
FETCH NEXT FROM MyCursor INTO #a, #b
END
CLOSE MyCursor
DEALLOCATE MyCursor
I have this query.
DECLARE #testvar varchar
DECLARE #rescount int
DECLARE MY_CURSOR CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
select distinct value from [table] where tablevalId = n
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO #testvar
WHILE ##FETCH_STATUS = 0
BEGIN
select #rescount = Count(value2) from [table] where value = #testvar and tablevalId = n
if #rescount > 1
BEGIN
select top (#rescount - 1) value2 from [table] where value = #testvar and tablevalId = n order by tablevalue3 desc --This is the value I want
END
else
BEGIN
select #rescount = 1 --This is a 'DO NOTHING'
END
FETCH NEXT FROM MY_CURSOR INTO #testvar
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
I am trying to get a few groups of records minus the first record in each group. The issue came in where the #rescount was not greater than 1. That is when the IF/ELSE was implemented.
Before the IF/ELSE, it would print the result but break when the #rescount was not greater than 1. Now, it prints nothing and tells me "The Query Was Completed Successfully".
Would anyone be able to advise on this?
Thanks
Why are you using cursors for this? Just use row_number():
select t.*
from (select t.*, row_number() over (partition by tablevalId, value
order by tablevalue3 desc) as seqnum
from [table] t
) t
where seqnum > 1;
You should avoid cursors if you do not need them.
I have a TSQL code that relies on a stored procedure to select a row.
When I'm implementing a more complex TSQL script that will select many rows based on a condition, instead of having one result set of x rows I'm ending up with x result sets containing one row.
My first question is: is it a concern or the performances are close to what I would get with one result set of x rows?
Second question: does anybody think that a temporary table where my stored procedure insert the result (instead of a select) should be faster?
Edit:
Basically this stored procedure select all the items of a given HierarchicalObject.
ALTER PROCEDURE [dbo].[MtdMdl_HierarchicalObject_Collection_Items]
#relatedid int
AS
BEGIN
SET NOCOUNT ON
declare #curkeyid int
declare cur CURSOR static read_only LOCAL
for select distinct [Id] from MtdMdl_Item where [Owner] = #relatedid
open cur
fetch next
from cur into #curkeyid
while ##FETCH_STATUS = 0
BEGIN
-- select the item row from its ID
exec MtdMdl_Item_ItemBase_Read #keyid = #curkeyid
fetch next
from cur into #curkeyid
END
close cur
deallocate cur
END
ALTER PROCEDURE [dbo].[MtdMdl_Item_ItemBase_Read]
#keyid int
AS
BEGIN
SET NOCOUNT ON
SELECT TOP(1) [Id], [TimeStamp], [Name], [Owner], [Value]
FROM [MtdMdl_Item]
WHERE ([Id]=#keyid)
ORDER BY TimeStamp Desc
END
For sure you should better place all single output rows into resulting temporary table before selecting final recordset. There is no reason currently in your code to return one recorset containing all separate rows from iteration over cursor with sp;
Your MtdMdl_Item_ItemBase_Read is relevant a bit because after turning it into function you can avoid sp+cursor and complete the task with one single query using inline function.
upd
According to your data structure I understand that your [Id] is not unique which is source of confusing.
There are many ways to do what you need but here is example of one query even avoiding CTE for temporary result:
DECLARE #relatedid int = 2
SELECT top(1) WITH ties
[Id], [TimeStamp], [Name], [Owner], [Value]
FROM MtdMdl_Item
WHERE [Owner]=#relatedid
ORDER BY row_number() over(partition BY [Id] ORDER BY [TimeStamp] DESC)
Consider this SQL Fiddle as demo.
upd2
Example with inline table function:
CREATE FUNCTION MtdMdl_Item_ItemBase_Read (#keyid int)
RETURNS TABLE
AS
RETURN
(
SELECT TOP(1) [Id], [TimeStamp], [Name], [Owner], [Value]
FROM [MtdMdl_Item]
WHERE ([Id]=#keyid)
ORDER BY TimeStamp Desc
)
GO
DECLARE #relatedid int = 2
SELECT DISTINCT A.[Id],B.* FROM MtdMdl_Item A
OUTER apply (SELECT * FROM MtdMdl_Item_ItemBase_Read(A.[Id])) B
WHERE A.[Owner] = #relatedid
SQL Fiddle 2
Your answer is in below link you should use GROUP BY instead of DISTINCT
SQL/mysql - Select distinct/UNIQUE but return all columns?
And in below line of your code enter list of columns you want in your result
declare cur CURSOR static read_only LOCAL
for select distinct [Id] from MtdMdl_Item where [Owner] = #relatedid
So your query will be
declare cur CURSOR static read_only LOCAL
for select rows,you,want,in,result from MtdMdl_Item where [Owner] = #relatedid Order By [column name you want to be distinct]
I have a stored procedure that searches a table which has about 200000+ rows with full text FREETEXT.
Here is the basics of it:
declare #searchKey varchar(150)
if #searchKey Is Null OR LEN(#searchKey)=0
Set #searchKey='""';
Set #searchKey='car';
declare #perPage int
Set #perPage=40
declare #pageNo int
Set #pageNo=1
declare #startIndex int,#endIndex int;
Set #startIndex=#perPage*#pageNo-#perPage+1;
Set #endIndex=#perPage*#pageNo;
Select totalItems
--i pull other colums as well
from (
Select Row_Number() over(order by CreateDate DESC) As rowNumber
,COUNT(*) OVER() as totalItems
--other columns are pulled as well
from MyTable P
Where
#searchKey='""'
OR FreeText((P.Title,P.Description),#searchKey)
) tempData
--where rowNumber>=#startIndex AND rowNumber<=#endIndex
where
rowNumber>=CASE WHEN #startIndex>0 AND #endIndex>0 THEN #startIndex ELSE rowNumber END
AND rowNumber<=CASE WHEN #startIndex>0 AND #endIndex>0 THEN #endIndex ELSE rowNumber END
order by rowNumber
The problem is its running slower then i would like it. Its taking about 3 seconds to load the page. Same page was loading in less then 1 sec when i was using like operator.
In my experience, full text index functions do not work well in a clause that contains an "OR" operator. I have had to get the same behavior by adjusting my query to use a UNION. Try this and see if you can get better performance.
declare #searchKey varchar(150)
if #searchKey Is Null OR LEN(#searchKey)=0
Set #searchKey='""';
Set #searchKey='car';
declare #perPage int
Set #perPage=40
declare #pageNo int
Set #pageNo=1
declare #startIndex int,#endIndex int;
Set #startIndex=#perPage*#pageNo-#perPage+1;
Set #endIndex=#perPage*#pageNo;
Select totalItems
--i pull other colums as well
from (
Select Row_Number() over(order by CreateDate DESC) As rowNumber
,COUNT(*) OVER() as totalItems
--other columns are pulled as well
from
(
select * from
MyTable A
Where
#searchKey='""'
UNION
select * from MyTable B
where FreeText((B.Title,B.Description),#searchKey)
) as innerTable
) tempData
--where rowNumber>=#startIndex AND rowNumber<=#endIndex
where
rowNumber>=CASE WHEN #startIndex>0 AND #endIndex>0 THEN #startIndex ELSE rowNumber END
AND rowNumber<=CASE WHEN #startIndex>0 AND #endIndex>0 THEN #endIndex ELSE rowNumber END
order by rowNumber
I am trying to see if its possible to perform Update within a cursor loop and this updated data gets reflected during the second iteration in the loop.
DECLARE cur CURSOR
FOR SELECT [Product], [Customer], [Date], [Event] FROM MyTable
WHERE [Event] IS NULL
OPEN cur
FETCH NEXT INTO #Product, #Customer, #Date, #Event
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT * FROM MyTable WHERE [Event] = 'No Event' AND [Date] < #DATE
-- Now I update my Event value to 'No Event' for records whose date is less than #Date
UPDATE MyTable SET [Event] = 'No Event' WHERE [Product] = #Product AND [Customer] = #Customer AND [Date] < #DATE
FETCH NEXT INTO #Product, #Customer, #Date, #Event
END
CLOSE cur
DEALLOCATE cur
Assume when the sql executes the Event column is NULL for all records
In the above sql, I am doing a select inside the cursor loop to query MyTable where Event value is 'No Event' but the query returns no value even though I am doing an update in the next line.
So, I am thinking if it is even possible to update a table and the updated data get reflected in the next iteration of the cursor loop.
Thanks for any help,
Javid
Firstly You shouldn't need a cursor here. Something like the following would have the same semantics (from a starting position where all Event are NULL) and be more efficient.
WITH T
AS (SELECT [Event],
RANK() OVER (PARTITION BY [Product], [Customer]
ORDER BY [Date] DESC) AS Rnk
FROM MyTable)
UPDATE T
SET [Event] = 'No Event'
WHERE Rnk > 1
Secondly regarding the question in the title to commit inside a cursor loop is the same as anywhere else. You just need a COMMIT statement. However if you aren't running this inside a larger transaction the UPDATE statement will be auto committed anyway.
Thirdly Your real question doesn't seem to be about commit anyway. It is about the cursor reflecting updates to the data on subsequent iterations. For the case in the question you would need a DYNAMIC cursor
Defines a cursor that reflects all data changes made to the rows in
its result set as you scroll around the cursor. The data values,
order, and membership of the rows can change on each fetch.
Not all queries support dynamic cursors. The code in the question would but without an ORDER BY it is undeterministic what order the rows would be processed in and thus whether you would see visible results. I have added an ORDER BY and an index to support this to allow a dynamic cursor to be used.
If you try the following you will see the cursor only fetches one row as the dates are processed in descending order and when the first row is processed the table is updated such that no more rows qualify for the next fetch. If you comment out the UPDATE inside the cursor loop all three rows are fetched.
CREATE TABLE MyTable
(
[Product] INT,
[Customer] INT,
[Date] DATETIME,
[Event] VARCHAR(10) NULL,
PRIMARY KEY ([Date], [Product], [Customer])
)
INSERT INTO MyTable
VALUES (1,1,'20081201',NULL),
(1,1,'20081202',NULL),
(1,1,'20081203',NULL)
DECLARE #Product INT,
#Customer INT,
#Date DATETIME,
#Event VARCHAR(10)
DECLARE cur CURSOR DYNAMIC TYPE_WARNING FOR
SELECT [Product],
[Customer],
[Date],
[Event]
FROM MyTable
WHERE [Event] IS NULL
ORDER BY [Date] DESC
OPEN cur
FETCH NEXT FROM cur INTO #Product, #Customer, #Date, #Event
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT #Product,
#Customer,
#Date,
#Event
-- Now I update my Event value to 'No Event' for records whose date is less than #Date
UPDATE MyTable
SET [Event] = 'No Event'
WHERE [Product] = #Product
AND [Customer] = #Customer
AND [Date] < #Date
FETCH NEXT FROM cur INTO #Product, #Customer, #Date, #Event
END
CLOSE cur
DEALLOCATE cur
DROP TABLE MyTable
Even if this worked, this would not guarantee the correct result since you miss an ORDER BY clause in your query.
Depending on this, all records, no records or any random subset of records could be updated.
Could you please explain in plain English what your stored procedure should do?
Use Below template
DECLARE #CCount int = 100
DECLARE #Count int = 0
DECLARE #id AS BigInt
DECLARE cur Cursor fast_forward for
SELECT t1.Id
FROM Table1 t1 WITH (NOLOCK) WHERE <Some where clause>
OPEN cur
Begin Tran
While (1=1)
Begin
Fetch next from cur into #id
If ##Fetch_Status <> 0
break
-- do some DML actions
Delete From Table1 WITH (ROWLOCK) where Id = #id
Set #count = #count + ##Rowcount
if (#count % #CCount = 0)
Begin
if (#count % 100 = 0)
Print 'Table1: DML action ' + Cast(#count as Varchar(15)) + ' rows'
-- for every 100 rows commit tran will trigger , and starts a new one.
While ##Trancount > 0 Commit Tran
Begin Tran
End
End
While ##Trancount > 0 Commit Tran
Close cur
Deallocate cur