I have stored proc sp_generate_insert which will generate insert scripts for the tables. When I run the stored Proc
from the management studio it runs fine. But when I run through stored proc as part of BCP utility I get this error.
'SQLState = 42000, NativeError = 536
Error = [Microsoft][SQL Native Client][SQL Server]Invalid length parameter passed to the SUBSTRING function.'
Execute dev.dbo.sp_generate_inserts 'auth' runs fine from management studio and generates inserts for auth table.
When I run the same proc as part of the following stored proc with bcp utility I get the error.
alter PROCEDURE INSERTTEST2 ( @FILEPATH NVARCHAR(50))
DECLARE @cmd varchar(2000)
set @cmd = 'bcp.exe "EXEC dev.dbo.SP_GENERATE_INSERTS auth" '
+ 'QUERYOUT' + ' ' +@filePath+ '.sql ' +'-S ' +
'NV-DEVSQL3' + ' -q ' + ' -c -T -e' + @filePath+'.log -o '
+ @filePath+ '_out.log'
select @cmd -- + '...'
EXEC master.dbo.xp_cmdShell @cmd
Any suggestions or inputs would help.
The problem lies within the proc, so we need to see that code.
Though usually, this error comes from statements where the length parameter in SUBSTRING becomes negative.
If you're dynamically trying to set how large chunk substring should take, and that variable becomes negative, then this error happens.
Since the problem seems to occur or not depending on method of connecting, it may suggest that there are different settings that may be the root cause.. (ie ANSI DEFAULTS etc)
Could this be it perhaps?
Thank you for you reply.
I dont know if the problem is setting defaults on the database or the connection, more so since the stored proc - sp_generate_scripts runs fine from the managment studio.
Anyways the code for stored proc is available at the following link
Any suggestions/inputs would help
I played around a bit with the proc and found some 'interesting' stuff...
I think your problem may be that you don't use the -d parameter in your bcp command, so you're not ending up in the right db.
The reason this matters may be the same that I found, but didn't notice at first...
(I tried it on SQL Server 2000).
First when compiled, there was a msg about not finding sys.sp_MS_marksystemobject, but the proc compiled anyway, so I tried it out.
Got the same message as you a couple of times, but found that only if I was in a db other than master. Made a usertable in master, then it worked. =/
So, fixed the 'sys.sp_MS_marksystemobject' to 'sp_MS_marksystemobject' and recompiled (since the former doesn't exist in 2000, only in 2005) and tried again. Now all is smooth, and it works like it's supposed to.
Apparently, the proc needs to be marked as a systemobject, else you may get these 'db-scope' issues, so check out if this is the problem.