I have problem when I run this procedure it gives me back an error trying to parse 'ok' to int but I dont want result to be int.
DECLARE #result_var nvarchar(max)
DECLARE #ParmDefinition NVARCHAR(max)
EXECUTE Sp_executesql
#definition,
#ParmDefinition,
#result_var = #result_var output;
Code that is changing #result_var
SET #result_var = ''ok'';
DECLARE #cursor CURSOR,
#name VARCHAR(100)
SET #cursor = CURSOR FOR
SELECT [NAME]
FROM [iflvs08].mds.mdm.lv_budget_employee
OPEN #cursor
FETCH next FROM #cursor INTO #name
WHILE ##FETCH_STATUS = 0
BEGIN
IF RIGHT(#Name, 1) = '' ''
begin
IF (Len(#result_var) > 1)
SET #result_var = #result_var + '','' + #name
ELSE
SET #result_var = #name
END
FETCH next FROM #cursor INTO #name
END
Looks like that you declared but didn't set up #ParmDefinition variable.
It should be like this:
DECLARE #ParmDefinition NVARCHAR(max)
SET #ParmDefinition = N'#result_var nvarchar(max) OUTPUT';
EXECUTE Sp_executesql
#definition,
#ParmDefinition,
#result_var=#result_var output ;
Related
How can I store the result of exec into a variable?
Here's my attempt at storing the data in a variable:
DECLARE #oltp VARCHAR(50) SET #oltp = 'emp'
DECLARE #wh VARCHAR(25) SET #wh = '_wh'
DECLARE #DatabaseName SYSNAME = #oltp + #wh;
DECLARE #oltp_data_days NVARCHAR(500)
DECLARE #Params NVARCHAR(500)
DECLARE #vi int
SET #oltp_data_days = N'(SELECT datediff(DAY, MIN(business_date), GETDATE()) AS keep_days
FROM '+QUOTENAME(#DatabaseName)+'..sales
WHERE business_date > ''1/1/2000 00:00:00'')'
EXEC sp_executesql #oltp_data_days = #oltp_data_days
, #Params = N'#vi INT OUTPUT'
, #vi = #vi OUTPUT
I am getting results as Null.
Like this:
declare #sql nvarchar(max) = N'set #vi = (select 3 as keep_days)'
declare #vi int
exec sp_executesql #sql, N'#vi int output', #vi = #vi output
select #vi
My problem is in the line with EXEC, all works except adding value to #counter.
If I execute something like below there is no problem:
EXEC('if exists(select * from '+ #table + ' where ' + #column + ' = ' + #someValue + ') print ''test''');
What to do to add value to the #counter? When I'm running below code I'm getting error:
Incorrect syntax near 0.
CREATE PROCEDURE testSP
AS
BEGIN
DECLARE testCursor CURSOR;
DECLARE #table NVARCHAR;
DECLARE #column NVARCHAR;
DECLARE #someValue NVARCHAR;
...
BEGIN
DECLARE testCursor2 CURSOR;
DECLARE #counter INT = 0;
...
BEGIN
DECLARE #result INT = 0;
EXEC( 'if exists(select * from '+ #table + ' where ' + #column + ' = ' + #someValue + ') set ' + #counter + '=1' );
IF (#counter > 0)
PRINT 'test';
...
END
...
END
END
go
You can use this.
CREATE PROCEDURE testSP
AS
BEGIN
DECLARE testCursor CURSOR;
DECLARE #table NVARCHAR;
DECLARE #column NVARCHAR;
DECLARE #someValue NVARCHAR;
...
BEGIN
DECLARE testCursor2 CURSOR;
DECLARE #counter INT = 0;
...
BEGIN
DECLARE #result INT = 0;
declare #sqlText nvarchar(max)= N'if exists(select * from '+ #table + ' where ' + #column + ' = ' + #someValue + ') SET #counter = 1'
DECLARE #ParmDefinition nvarchar(500) = N'#counter INT OUTPUT';
EXEC sp_executesql #sqlText, #ParmDefinition, #counter = #counter OUTPUT ;
if(#counter>0)
print 'test';
...
END
...
END
END
I am getting the 'Incorrect syntax near 'db_cursor' error when attempting to run the below query. Can someone please tell me why and where my error in the scripting? Thanks in advance!
declare #Sort varchar(4000)
declare #OpenDate datetime
declare #ret int;
set #Sort = 'All'
set #OpenDate = '1/1/2015'
IF (#Sort = 'All')
BEGIN
declare #Organization varchar(2500)
declare #OrgEmp int
Set #OrgEmp = 0
Set #ret = 0
declare db_cursor CURSOR FOR
SELECT distinct RTrim(Organization) + '|' as Oganization,
(select rpt.ufnGetEmployeePopulation(RTrim(Organization) + '|', #OpenDate)) as NumberofEmployee FROM dbo.CorpStartEndDates where EndDate >= #OpenDate
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #Organization, #OrgEmp
WHILE ##FETCH_STATUS = 0
BEGIN
Set #ret = #ret + #OrgEmp
FETCH NEXT FROM db_cursor INTO #Organization, #OrgEmp
END
CLOSE db_cursor
DEALLOCATE db_cursor
You're missing an END
if the cursor is inside the first BEGIN/END then put END after the DEALLOCATE
If the cursor is outside of the first begin/end, but the end before the OPEN
so..
declare #Sort varchar(4000)
declare #OpenDate datetime
declare #ret int;
set #Sort = 'All'
set #OpenDate = '1/1/2015'
IF (#Sort = 'All')
BEGIN
declare #Organization varchar(2500)
declare #OrgEmp int
Set #OrgEmp = 0
Set #ret = 0
declare db_cursor CURSOR FOR
SELECT distinct RTrim(Organization) + '|' as Oganization,
(select rpt.ufnGetEmployeePopulation(RTrim(Organization) + '|', #OpenDate)) as NumberofEmployee FROM dbo.CorpStartEndDates where EndDate >= #OpenDate
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #Organization, #OrgEmp
WHILE ##FETCH_STATUS = 0
BEGIN
Set #ret = #ret + #OrgEmp
FETCH NEXT FROM db_cursor INTO #Organization, #OrgEmp
END
CLOSE db_cursor
DEALLOCATE db_cursor
END
or
IF (#Sort = 'All')
BEGIN
declare #Organization varchar(2500)
declare #OrgEmp int
Set #OrgEmp = 0
Set #ret = 0
END
I have problem inserting sql script in the variable.I have this code
declare #result_var varchar(max);
SET #result_var='';
DECLARE #cursor CURSOR,
#name VARCHAR(100)
SET #cursor = CURSOR
FOR SELECT [NAME]
FROM [iflvs08].mds.mdm.lv_budget_employee
OPEN #cursor
FETCH next FROM #cursor INTO #name
WHILE ##FETCH_STATUS = 0
BEGIN
IF (RIGHT(#Name, 1) = ' ')
begin
IF( Len(#result_var) > 1 )
SET #result_var=#result_var + ','+ #name
ELSE
SET #result_var=#name
END
FETCH next FROM #cursor INTO #name
END
I want to insert into variable it but when try to insert it like this
set #sql='code here'
It get confuse whit this ' symbol and dont insert it
I always get error like this
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '
begin
IF( Len(#result_var) > 1 )
SET #result_var=#result_var + '.
basically you have to replace all ' symbols with ''
set #sql = '
declare #result_var varchar(max);
SET #result_var='''';
DECLARE #cursor CURSOR,
#name VARCHAR(100)
SET #cursor = CURSOR
FOR SELECT [NAME]
FROM [iflvs08].mds.mdm.lv_budget_employee
OPEN #cursor
FETCH next FROM #cursor INTO #name
WHILE ##FETCH_STATUS = 0
BEGIN
IF (RIGHT(#Name, 1) = '' '')
begin
IF( Len(#result_var) > 1 )
SET #result_var=#result_var + '',''+ #name
ELSE
SET #result_var=#name
END
FETCH next FROM #cursor INTO #name
END'
I know this question has been asked, and I already found some solutions in internet.. but I still can not make it work properly.
So.. I have to make a SELECT query and store the result in a variable (I DONT want a table variable).
My problem is that the name of the table is also a variable. The table name changes accordingly to a WHILE, here is my code:
DECLARE #numRecord INT;
DECLARE #maxMacNumber INT;
SET #maxMacNumber = 500;
DECLARE #mac INT;
SET #mac = 0;
DECLARE #res FLOAT;
DECLARE #ap INT;
SET #ap = 0;
DECLARE #apString VARCHAR(2);
DECLARE #numRecordString VARCHAR(20);
DECLARE #tablename VARCHAR(500);
DECLARE #sql NVARCHAR(500);
DECLARE #varDefinition NVARCHAR(200);
WHILE #mac <= #maxMacNumber
BEGIN
SET #numRecord = 6 + #mac * 390;
SET #ap = 0;
WHILE #ap < 2
BEGIN
SELECT #apString = CONVERT(VARCHAR,#ap);
SELECT #numRecordString = CONVERT(VARCHAR, #numRecord);
SELECT #rssiString = CONVERT(VARCHAR, #rssi);
SET #tablename = 'APDB.dbo.AP' + #apString;
SET #sql = 'SELECT RSSI FROM ' + #tablename + ' WHERE ID=' + #numRecordString;
SET #varDefinition = N'#res FLOAT OUTPUT';
EXEC sp_executesql #sql, #varDefinition, #res = #res OUTPUT;
PRINT #res;
-- HERE I WILL DO SOMETHING WITH #res
END;
END;
The problem is that it doesn't print anything when I do PRINT #res...
This is the relevant SQL code:
SET #sql = 'SELECT RSSI FROM ' + #tablename + ' WHERE ID=' + #numRecordString;
SET #varDefinition = N'#res FLOAT OUTPUT';
EXEC sp_executesql #sql, #varDefinition, #res = #res OUTPUT;
PRINT #res;
You are never setting #res in the SQL. Try this:
SET #sql = 'SELECT #res = RSSI FROM ' + #tablename + ' WHERE ID=' + #numRecordString;
SET #varDefinition = N'#res FLOAT OUTPUT';
EXEC sp_executesql #sql, #varDefinition, #res = #res OUTPUT;
PRINT #res;