Dynamic Cursor from the procedure argument - sql

I have a procedure that takes an argument. And I want to loop a cursor through the query based on that argument.
For example:
the argument is passed: salary
the cursor should be (select salary from emp) and loop through it.
How should I do that ?

DECLARE #ARGUMENT VARCHAR(50) = 'salary';
DECLARE #CURSOR_BODY VARCHAR(MAX) = '
declare #variable varchar(max)
declare cur cursor for
select '+ #argument + ' from emp
OPEN cur;
FETCH NEXT FROM cur INTO #variable
WHILE ##FETCH_STATUS=0
BEGIN
select #variable
FETCH NEXT FROM cur INTO #variable;
END
CLOSE cur
DEALLOCATE cur'
print #cursor_body
EXEC (#CURSOR_BODY)
Try sth. like this

Related

Delete multiple records from table through cursor in sql server

there are number of test IP's which I would like to remove through system defined sp
exec sp_delete_firewall_rule from sys.firewall_rules table in sql server
I am using below cursor but its not working
declare #name nvarchar(max)
declare cur CURSOR LOCAL for
select #name from sys.firewall_rules where [name] like '%TestIP%'
open cur
fetch next from cur into #name
while ##FETCH_STATUS = 0 BEGIN
exec sp_delete_firewall_rule #name
fetch next from cur into #name
END
close cur
deallocate cur
It worked for me, you just need to change a couple of things in your code.
In the select list include the table ColumnName [name] instead of variable. You did not pass any value to the variable so this gives a NULL result.
Include SP parameter while executing exec sp_delete_firewall_rule #name = #name1;
I have these IP’s in my firewall rules:
With the below code I am deleting the IP’s which has a name like TestIP1.
DECLARE #name1 nvarchar(128);
DECLARE MyCursor CURSOR FOR
SELECT [name] from sys.firewall_rules where [name] like '%TestIP1%';
OPEN MyCursor;
FETCH FROM MyCursor into #name1
WHILE ##FETCH_STATUS = 0 BEGIN
EXEC sp_delete_firewall_rule #name = #name1 ;
FETCH next from MyCursor into #name1
END
CLOSE MyCursor;
DEALLOCATE MyCursor;
GO
Now the result shows only 1 IP which is not included in the above delete list.

How do I loop through the results of a SELECT statement sql and use the result for stored procedure?

I want to use the result of a select query into a stored procedure by using cursor but it doesn't work.
Here is my code:
DECLARE #NumberPhone varchar(50)
DECLARE CUR CURSOR STATIC FOR
SELECT MobilePhone
FROM info_client
OPEN CUR
FETCH NEXT FROM CUR INTO #NumberPhone
WHILE ##FETCH_STATUS = 0
BEGIN
DECLARE #return_value int, #idCli int, #sCode varchar(2)
EXEC #return_value = StoredP_Test
#sLignePhone = #NumberPhone,
#sIMEI ='00000000000000000',
#idCli = #idCli OUTPUT,
#sCode = #sCode OUTPUT
SELECT #idCli as N'#idCli',
#sCode as N'#sCode'
FETCH NEXT FROM CUR INTO #NumberPhone
SELECT 'Return Value' = #return_value
END
CLOSE CUR
DEALLOCATE CUR
This code duplicate the same result as the number of line of the select query.
Your code has an initial fetch of
FETCH NEXT FROM CUR INTO #NumberPhone
Your code has a fetch next inside the loop
FETCH NEXT FROM CUR INTO #sLigneAssValue
Didn't you want to fetch next into the #NumberPhone variable?
The #NumberPhone variable will never change.

SQL Server 2014 - sp_describe_cursor_column for dynamic queries issue

Basically I want to know columns/aliases of result set from dynamic query. I tried to use sp_describe_cursor_column but without success.
It returns me that cursor does not exits. But I can fetch values from such cursor...
The code is :
ALTER PROC TestProc
AS
DECLARE #dynamicSQL nvarchar(200)
-- Have code that will construct the dynamic SQL
SET #dynamicSQL = ' select table_name, TABLE_TYPE from INFORMATION_SCHEMA.TABLES'
-- The cursor that will be filled by the dynamic SQL
DECLARE #outputCursor CURSOR
-- Create the dynamic SQL to fill a CURSOR instead
SET #dynamicSQL = 'SET #outputCursor = CURSOR FORWARD_ONLY STATIC FOR ' +
#dynamicSQL + ' ; OPEN #outputCursor'
-- Execute dynamic sql
exec sp_executesql -- sp_executesql will essentially create a sproc
#dynamicSQL, -- The SQL statement to execute (body of sproc)
N'#outputCursor CURSOR OUTPUT', -- The parameter list for the sproc: OUTPUT CURSOR
#outputCursor OUTPUT -- The parameter to pass to the sproc: the CURSOR
declare #Report cursor
exEC sp_describe_cursor_columns
#cursor_return = #Report OUTPUT
,#cursor_source = N'local'
,#cursor_identity = N'outputCursor';
-- Code that will just output the values from the cursor
DECLARE #tableName nvarchar(200), #table_type nvarchar(200);
FETCH NEXT FROM #outputCursor INTO #tableName, #table_type
-- Loop while there're more things in the cursor
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT #tableName
FETCH NEXT FROM #outputCursor INTO #tableName, #table_type
END
-- Be nice, close & deallocate cursor
CLOSE #outputCursor
DEALLOCATE #outputCursor
And this is the result:
Msg 16916, Level 16, State 4, Procedure sp_describe_cursor_columns,
Line 23 A cursor with the name 'outputCursor' does not exist.
DATABASE_UPDATE
SYSTEM_CONFIGURATION ....
I want as result to see table_name , table_type.
Don't tell me that I can just extarct it from string, becasue user may send select * from xxxx.
I found some other way how to extract description of result set for dynamic queries.
declare #Table nvarchar(200) ;
DECLARE #dynamicSQL nvarchar(200)
SET #dynamicSQL = 'select table_name, TABLE_TYPE from INFORMATION_SCHEMA.TABLES'
SELECT #Table = COALESCE(#Table + ', ', '') + Name
FROM sys.dm_exec_describe_first_result_set
(#dynamicSQL, NULL,1)
print #Table

Sql cursor in infinite loop. What is wrong in this code?

Hi I am trying to loop for each employee id in table.
BEGIN
declare #empId nvarchar(50)
declare cur Cursor LOCAL for
select EmpId from EmployeeMaster
open cur
fetch next from cur into #empId
while ##FETCH_STATUS =0
begin
select #empId
end
close cur
END
This is my query in stored procedure. What is wrong with this? it is giving me first employee id within infinite loop.
If i check while ##FETCH_STATUS =1 then no output given. just saying
Command(s) completed successfully.
You need to add fetch command after select
BEGIN
declare #empId nvarchar(50)
declare cur Cursor LOCAL for
select EmpId from EmployeeMaster
open cur
fetch next from cur into #empId
while ##FETCH_STATUS =0
begin
select #empId
fetch next from cur into #empId
end
close cur
END

Return a row from all tables and all databases?

How would be the SQL SELECT statement for returning a row from all data bases on the server, but just from a specific table and some of the columns of the table.
Or, in pseudo code, something like this:
for each(database)
{
return database.column.row;
}
This was not tested, but it should be enough to get you started.
DECLARE #name as NVARCHAR(128);
DECLARE #sql AS NVARCHAR(max);
DECLARE c_Cursor CURSOR FOR
SELECT databases.name
FROM sys.databases
WHERE databases.database_id > 4
ORDER BY databases.name;
OPEN c_Cursor;
FETCH NEXT FROM c_Cursor
INTO #name;
WHILE ##FETCH_STATUS = 0
BEGIN
SET #sql = N'SELECT table.* FROM ' + QUOTENAME(#name) + N'.dbo.table';
EXEC #sql
FETCH NEXT FROM c_Cursor
INTO #name;
END;
CLOSE c_Cursor;
DEALLOCATE c_Cursor;