I am declaring a dynamic cursor due to a nesting of the stored procedure based on a parent-child relationship of the data that can go multiple levels and vary daily when processed. The stored procedure works for the parent call, however the process of this procedure for child data causes a message stating that the "Cursor is not open." This message occurs on the Fetch which is immediately after checking to be sure the cursor is open.
DECLARE #OutCur CURSOR;
DECLARE #curName as NVARCHAR(MAX);
...
SET #curName = LEFT(replace(replace(CONVERT (time, GETDATE()),':',''),'.',''),8);
SET #sqlCommand = 'SELECT a.myfields FROM mytable a;
SET #sqlCommand = 'SET #cursor' + #curName + ' = CURSOR LOCAL FAST_FORWARD FOR ' + #sqlCommand + ' OPEN #cursor' + #curName + ';'
SET #curName = '#cursor' + #curName + ' cursor output';
EXEC sys.sp_executesql #sqlCommand,#curName,#OutCur output
IF CURSOR_STATUS('global','#OutCur')=-1
OPEN #OutCur;
FETCH NEXT FROM #OutCur INTO #name,#type
Thanks in advance for the input.
if you uncomment close+deallocate - script works fine:
GO
DECLARE #OutCur CURSOR;
DECLARE #curName as NVARCHAR(MAX), #sqlCommand nvarchar(max), #name varchar(100), #type varchar(100)
declare #i int = 0
while #i < 5
begin
SET #curName = LEFT(replace(replace(CONVERT (time, GETDATE()),':',''),'.',''),8);
SET #sqlCommand = 'SELECT ''111'' as name, ''cc'' as type; '
SET #sqlCommand = 'SET #cursor' + #curName + ' = CURSOR LOCAL FAST_FORWARD FOR ' + #sqlCommand + ' OPEN #cursor' + #curName + ';'
SET #curName = '#cursor' + #curName + ' cursor output';
EXEC sys.sp_executesql #sqlCommand,#curName,#OutCur output
FETCH NEXT FROM #OutCur INTO #name,#type
select #name, #type
--close #OutCur
--deallocate #OutCur
set #i += 1
end
GO
Related
43/5000
First of all I apologize for my bad english...
I am trying to write the insert procedures of all tables with a single procedure. But I have a problem like this.
DECLARE #jsondata varchar(MAX)
Set #jsondata='[{"RecordId":1,"CreatedUser":0,"CreatedDate":"2020-03-26T14:49:21.210","UpdatedDate":"2020-03-26T14:57:33.420","UpdatedUser":0,"Status":true,"IsDeleted":false,"Name":"Oyun Konsolları","Icon":"videogame_asset","Description":"Oyun Konsolları","Order":1}]';
DECLARE #cn nvarchar(50)
DECLARE #dt nvarchar(50)
DECLARE #ml nvarchar(50)
DECLARE #inserttext varchar(MAX)
DECLARE #selecttext varchar(MAX)
DECLARE #jsoncol varchar(MAX)
DECLARE #tablename varchar(50)
SET #tablename = 'Categories'
SET #inserttext = ' INSERT INTO '+#tablename+' ( ';
SET #selecttext = ' SELECT ';
SET #jsoncol = ') WITH (';
DECLARE #schema nvarchar(max) = N''
DECLARE MY_CURSOR CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT
c.name 'Column Name',
t.Name 'Data type',
c.max_length 'Max Length'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
WHERE
c.object_id = OBJECT_ID(#tablename)
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO #cn,#dt,#ml
WHILE ##FETCH_STATUS = 0
BEGIN
IF(#cn NOT IN('CreatedUser','CreatedDate','UpdatedDate','UpdatedUser','Status','IsDeleted','RecordId','Status','IsDeleted'))
BEGIN
--Do something with Id here
SET #inserttext = #inserttext + '['+#cn + '], ';
SET #selecttext = #selecttext + '['+ #cn + '], ';
IF(#dt = 'varchar' OR #dt='nvarchar' )
BEGIN
SET #jsoncol = #jsoncol + '['+#cn + '] ' + #dt + ' (' +#ml + '), '
END
ELSE
BEGIN
SET #jsoncol = #jsoncol + '['+#cn + '] ' + #dt +', '
END
END
FETCH NEXT FROM MY_CURSOR INTO #cn,#dt,#ml
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
SET #jsoncol = LEFT(#jsoncol, LEN(#jsoncol) - 1)
SET #inserttext = LEFT(#inserttext, LEN(#inserttext) - 1)
SET #selecttext = LEFT(#selecttext, LEN(#selecttext) - 1)
SET #inserttext =#inserttext + ' )';
SET #jsoncol = #jsoncol + ' )';
EXEC( #inserttext + ' '+ #selecttext + ' ' +' FROM OPENJSON('+#jsondata+ #jsoncol);
Error i get after running :
Msg 103, Level 15, State 4, Line 1 The identifier that
starts with
'{"RecordId":1,"CreatedUser":0,"CreatedDate":"2020-03-26T14:49:21.210","UpdatedDate":"2020-03-26T14:57:33.420","UpdatedUser":0,"S'
is too long. Maximum length is 128.
Completion time: 2020-09-25T10:42:41.2474477+03:00
29/5000
Is it possible ?
in short, is it possible to insert into tables from json as dynamic exec
declare #sql nvarchar(max) = #inserttext + ' '+ #selecttext + ' ' +' FROM OPENJSON(#thejsondata'+ #jsoncol;
exec sp_executesql #stmt=#sql, #params=N'#thejsondata nvarchar(max)', #thejsondata = #jsondata;
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
USE KronosNET22
GO
Create procedure eventossucursales4
#id nvarchar(max),
#dia nvarchar(max)
as
begin
declare #sqlstring nvarchar(max)
set #sqlstring = 'Select Code From ' + #dia + ' WHERE idObject = ''' + #id + ''' AND (CODE = ''TFHi2'' OR CODE = ''E603'')'
EXEC sp_executesql #sqlstring, #id,#dia
end
GO
Execute eventossucursales4 'E4211537-09CD-45F2-BB5F-F20F642DE676','ObjectSignal_2016_05_23 '
Error:
Mens. 102, Nivel 15, Estado 1, Línea 1
Sintaxis incorrecta cerca de 'E4211537'.
Can someone help me to figure it out why its showing a mistake in the declaration of the variable?
You could eliminate passing the parameters as someone commented. If you want to make it work as is, you need to add the parameter list. I was able to get it working this way.
alter procedure eventossucursales4
#id nvarchar(max),
#dia nvarchar(max)
as begin
declare #sqlstring nvarchar(max)
declare #ParmDefinition nvarchar(500) = '#dia nvarchar(max), #id nvarchar(max)'
set #sqlstring = 'Select Code From ' + #dia + ' WHERE idObject = ''' + #id + ''' AND (CODE = ''TFHi2'' OR CODE = ''E603'')'
exec sp_executesql #sqlstring, #ParmDefinition, #id = #id, #dia = #dia
end
GO
Execute eventossucursales4 'E4211537-09CD-45F2-BB5F-F20F642DE676','ObjectSignal_2016_05_23 '
I am trying to change a stored procedure in our SQL environment which is used to create backup jobs remotely. Below is the procedure
/****** Object: StoredProcedure [dbo].[CreateBackupJobGroupBFull] Script Date: 8/06/2016 3:18:25 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateBackupJobGroupAFull]
(#servername nvarchar(100))
AS
declare #commandstr nvarchar(max)
DECLARE #SQL nvarchar(4000)
DECLARE #TableExists bit
DECLARE #recp nvarchar(100)
DECLARE #error_msg varchar(300)
declare #groupAfull nvarchar(150)
declare #schedulestr nvarchar(150)
DECLARE #pripathstr VARCHAR(1256)
DECLARE #secpathstr VARCHAR(1256)
declare #defaultscheduleStart varchar(30)
declare #gstr varchar(5)
declare #g varchar(1)
set #groupAfull = 'DBABackupJobGroupA_Full'
set #recp = '**************'
set #defaultscheduleStart = '230000'
set #gstr = '_GA_F'
set #g = 'A'
SET #SQL = 'SELECT #TableExists = CASE WHEN TableExists = 0 THEN 0 ELSE 1 END
FROM OPENQUERY(' + QUOTENAME(#servername)
+ ', ''SELECT TableExists = COUNT(*)
from msdb.dbo.sysjobs
WHERE name = '''' + #groupAfull + '''''');';
begin try
EXECUTE sp_executesql #SQL, N'#TableExists BIT OUTPUT', #TableExists OUT;
END TRY
BEGIN CATCH
print 'error message is:'
print error_message()
set #error_msg='Error occurred when executing CreatBackupJob against server ' + #ServerName + '. Failed to check control table locally. The error message is: ' + error_message()
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Internal mail',
#recipients = #recp,
#body = #error_msg ,
#subject = 'Error occurred on reporting server' ;
END CATCH
IF (#TableExists = 0 )
BEGIN
--add new job for the instance
set #sql = 'if not exists (select * from [' + #ServerName + '].msdb.dbo.sysjobs where name = ''' + #groupAfull + ''')
BEGIN
EXEC [' + #ServerName + '].msdb.dbo.sp_add_job
#job_name = ''' + #groupAfull + ''',
#enabled = 1,
#description = N''DBA backup job. DO NOT REMOVE. V1.5 (2015-06-16)'',
#owner_login_name = N''sa''
EXEC [' + #ServerName + '].msdb.dbo.sp_add_jobserver
#job_name = ''' + #groupAfull + ''',
#server_name = ''(local)''
end
'
begin try
EXEC sp_executeSQL #sql
END TRY
BEGIN CATCH
print 'error message is:'
print error_message()
set #error_msg='Error occurred when executing CreatBackupJob against server ' + #ServerName + '. Failed to create new job'+ #groupAfull +'. The error message is: ' + error_message()
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Internal mail',
#recipients = #recp,
#body = #error_msg ,
#subject = 'Error occurred on reporting server' ;
END CATCH
SET #pripathstr = (SELECT Top 1 [PrimDestinationfull] FROM [SQLADMIN].[dbo].[BackupDestination])
SET #secpathstr = (SELECT Top 1 [SecDestinationfull] FROM [SQLADMIN].[dbo].[BackupDestination])
--add job step 1 'test'
set #commandstr = '
DECLARE #dbname VARCHAR(150) -- database name
DECLARE #pripath VARCHAR(1256) -- path for backup files
DECLARE #secpath VARCHAR(1256) -- path for backup files
DECLARE #path VARCHAR(1256) -- path for backup files
DECLARE #fileName VARCHAR(1256) -- filename for backup
DECLARE #fileDate VARCHAR(40) -- used for file name
DECLARE #DBGROUP varchar(1)
DECLARE #filecount int
DECLARE #lastbackuptime datetime
SET #DBGROUP = ''''' + #g + '''''
IF not EXISTS (SELECT [name] FROM msdb.sys.tables WHERE [name] = ''''DBBackupControlTbl'''' )
begin
raiserror(''''Control table not found!'''', 20, -1) with log
return
end
else
if (select count(*) from msdb.dbo.DBBackupControlTbl where BackupGroup = #DBGROUP) > 0
-- specify database backup directory
SET #pripath = ''''' + #pripathstr + '''''
SET #secpath = ''''' + #secpathstr + '''''
if object_id(''''tempdb.dbo.#fileExist'''') is not null
drop table #fileExist
create table #fileExist (
fileExists int,
fileIsDir int,
parentDirExists int
)
insert into #fileExist
exec xp_fileexist #pripath
if object_id(''''tempdb.dbo.#fileExist2'''') is not null
drop table #fileExist2
create table #fileExist2 (
fileExists int,
fileIsDir int,
parentDirExists int
)
insert into #fileExist2
exec xp_fileexist #secpath
if (select count(*) from #fileExist where fileIsDir = 1) > 0
begin
set #path = #pripath
end
ELSE
begin
if (select count(*) from #fileExist2 where fileIsDir = 1) > 0
begin
set #path = #secpath
end
ELSE
begin
raiserror(''''None of the backup directory can be accessed at the moment!'''', 20, -1) with log
return
end
end
set #filecount = 999
while #filecount >= 15
begin
IF OBJECT_ID(''''tempdb..#DirectoryTree'''') IS NOT NULL
DROP TABLE #DirectoryTree;
CREATE TABLE #DirectoryTree (
id int IDENTITY(1,1)
,subdirectory nvarchar(512)
,depth int
,isfile bit);
INSERT #DirectoryTree (subdirectory,depth,isfile)
EXEC master.sys.xp_dirtree #path,1,1;
select #filecount = COUNT(*) FROM #DirectoryTree
WHERE isfile = 1 AND RIGHT(subdirectory,4) = ''''.sts''''
if #filecount >= 15
begin
print ''''Wait for status file to be cleared.''''
waitfor delay ''''00:00:10''''
end
else
begin
print ''''Backing up now.''''
DECLARE #statusfile AS VARCHAR(150)
DECLARE #cmd AS VARCHAR(150)
-- specify filename format
SELECT #fileDate = replace(replace(CONVERT(VARCHAR(40),GETDATE(),120), '''':'''', ''''-'''' ), '''' '''', ''''T'''')
set #statusfile = #path + replace(##SERVERNAME, ''''\'''', ''''-'''') + ''''_'''' + #fileDate + '''''+ #gstr + ''''' + ''''.STS''''
set #cmd = ''''echo '''' + #filedate + '''' > '''' + #statusfile
EXECUTE Master.dbo.xp_CmdShell #Cmd
DECLARE db_cursor CURSOR FOR
select databasename from msdb.dbo.DBBackupControlTbl where BackupGroup = #DBGROUP
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #dbname
WHILE ##FETCH_STATUS = 0
BEGIN
SET #fileName = #path + replace(##SERVERNAME, ''''\'''', ''''-'''') + ''''_'''' + #dbname + ''''_'''' + #fileDate + '''''+ #gstr + ''''' + ''''.BAK''''
BACKUP DATABASE #dbname TO DISK = #fileName
FETCH NEXT FROM db_cursor INTO #dbname
END
CLOSE db_cursor
DEALLOCATE db_cursor
set #cmd = ''''del '''' + #statusfile
EXECUTE Master.dbo.xp_CmdShell #Cmd
end
continue
end
'
print #commandstr
set #sql = 'if not exists (select * from [' + #ServerName + '].msdb.dbo.sysjobsteps where step_name = ''' + #groupAfull + ''')
BEGIN
EXEC [' + #ServerName + '].msdb.dbo.sp_add_jobstep
#job_name = ''' + #groupAfull + ''',
#step_name = ''' + #groupAfull + ''',
#subsystem = N''TSQL'',
#command = N''' + #commandstr + ''',
#retry_attempts = 3,
#retry_interval = 1,
#on_success_action = 1 ,
#on_fail_action= 2;
end
'
begin try
EXEC sp_executeSQL #sql
END TRY
BEGIN CATCH
print 'error message is:'
print error_message()
set #error_msg='Error occurred when executing CreatBackupJob against server ' + #ServerName + '. Failed to create new job step '+ #groupAfull +'. The error message is: ' + error_message()
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Internal mail',
#recipients = #recp,
#body = #error_msg ,
#subject = 'Error occurred on reporting server' ;
END CATCH
--add the schedule
set #schedulestr = #groupAfull + '_Schedule'
set #sql = 'if not exists (select * from [' + #ServerName + '].msdb.dbo.sysschedules where name = ''' + #schedulestr + ''')
BEGIN
EXEC [' + #ServerName + '].msdb.dbo.sp_add_schedule
#schedule_name = ''' + #schedulestr + ''',
#enabled=1,
#freq_type=4,
#freq_interval=1,
#freq_subday_type=1,
#freq_subday_interval=0,
#freq_relative_interval=0,
#freq_recurrence_factor=1,
#active_start_date=20140819,
#active_end_date=99991231,
#active_start_time= '+ #defaultscheduleStart +',
#active_end_time=235959
end
'
--print #sql
begin try
EXEC sp_executeSQL #sql
END TRY
BEGIN CATCH
print 'error message is:'
print error_message()
set #error_msg='Error occurred when executing CreatBackupJob against server ' + #ServerName + '. Failed to create new job schedule '+ #groupAfull +'. The error message is: ' + error_message()
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Internal mail',
#recipients = #recp,
#body = #error_msg ,
#subject = 'Error occurred on reporting server' ;
END CATCH
--attach the schedule
set #sql = 'EXEC [' + #ServerName + '].msdb.dbo.sp_attach_schedule
#job_name = ''' + #groupAfull + ''',
#schedule_name = ''' + #schedulestr + '''
'
begin try
EXEC sp_executeSQL #sql
END TRY
BEGIN CATCH
print 'error message is:'
print error_message()
set #error_msg='Error occurred when executing CreatBackupJob against server ' + #ServerName + '. Failed to attach the schedule to the job '+ #groupAfull +'. The error message is: ' + error_message()
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Internal mail',
#recipients = #recp,
#body = #error_msg ,
#subject = 'Error occurred on reporting server' ;
END CATCH
end
GO
The job created is created with 3 retries, so if backup fails it retires and then backs up all the database again irrespective where it failed. I am trying to change the procedure so that when the job fails it runs backup for databases which have failed. I have written below code to replace where it backups up database.
*#lastbackuptime is declared
BEGIN
SET #lastbackuptime = (select max(backup_finish_date) from msdb.dbo.backupset where database_name=#dbname and type=''''D'''')
if (Select datediff(hour,#lastbackuptime,getdate()))> 18
Begin
SET #fileName = #path + replace(##SERVERNAME, ''''\'''', ''''-'''') + ''''_'''' + #dbname + ''''_'''' + #fileDate + '''''+ #gstr + ''''' + ''''.BAK''''
BACKUP DATABASE #dbname TO DISK = #fileName
End
else
Begin
Print ''''Backup exists for database ''''
End
FETCH NEXT FROM db_cursor INTO #dbname
END
When I make this changes from job step , query parses fine and job runs fine. But when i put this in this procedure and try to create jobs remotely it fails with syntax error. As it is dynamic sql it does not provide me any clue where is the syntax error.
Would be great if someone can spot it.
Regards,
Sid
Questions of "Dynamic SQL not working" type come up often and sometimes (as in your case) can be very hard to answer, as we cannot execute the code you have supplied.
So this answer describes the method of debugging dynamic SQL.
Add a PRINT statement to print your dynamic SQL string just before it gets executed. When using PRINT command long strings can be truncated, so you need to use workarounds (How to print VARCHAR(MAX) using Print Statement? provides a few solutions).
Once you get your complete string printed check it for syntax errors and/or just run it. You can check code for syntax error by pasting it into a new query window and running "Parse" (Ctrl+F5).
Once the error in dynamic SQL is identified you need to find the place in your dynamic SQL generator code that produced this error and fix it there.
Repeat this process until no errors left.
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;