July 18, 2007 at 11:45 am
Guys,
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))
AS
DECLARE @cmd varchar(2000)
BEGIN
set @cmd = 'bcp.exe "EXEC vbaicms_dev.dbo.SP_GENERATE_INSERTS auth" '
+ 'QUERYOUT' + ' ' +@filePath+ '.sql ' +'-S ' +
'NV-DEVSQL3\ASTRO' + ' -q ' + ' -c -T -e' + @filePath+'.log -o '
+ @filePath+ '_out.log'
select @cmd -- + '...'
EXEC master.dbo.xp_cmdShell @cmd
END
Any suggestions and inputs would be helpful.
Thanks
July 18, 2007 at 12:13 pm
July 18, 2007 at 12:32 pm
Sp_generate_inserts runs fine from query analyzer - I dont think there is a problem with that its only when I usd as part of bcp utility it gives me error.
Basically the purpose of this sp is to generate insert statements for the table which is passed as variable.
I have tried with quotes on it still gives me the same error.
SQLState = 42000, NativeError = 536
Error = [Microsoft][SQL Native Client][SQL Server]Invalid length parameter passed to the SUBSTRING function.
The code for sp_generate_inserts is at the following link
http://vyaskn.tripod.com/code/generate_inserts_2005.txt
Thanks
July 18, 2007 at 12:39 pm
July 18, 2007 at 12:48 pm
I get the following
bcp.exe "EXEC vbaicms_dev.dbo.SP_GENERATE_INSERTS 'auth'" QUERYOUT C:\AICMS.sql -S NV-DEVSQL3 -q -c -T -eC:\AICMS.log -o C:\AICMS_out.log
July 19, 2007 at 7:46 am
any ideas guys ??
July 19, 2007 at 9:49 am
am,
I agree with mrpolecat. It is much easier to help someone out when all the code is posted. Others on the forum are much more likely to respond if you provide the create statements for sprocs and tables so we can just test it in our own development environments rather than trying to guess what your code is. That being said, I've got some sample code below. My attempt to write a sample of what you're doing.
Here's a comparison of your EXEC and my EXEC, they're pretty close.
bcp.exe "EXEC vbaicms_dev.dbo.SP_GENERATE_INSERTS 'auth'" QUERYOUT C:\AICMS.sql -S NV-DEVSQL3 -q -c -T -eC:\AICMS.log -o C:\AICMS_out.log
bcp.exe "EXEC cif.dbo.IFGenerateInsert 'LaborGroups'" QUERYOUT c:/temp/IFInsertTest.sql -S development -U XXX -P XXX -c -T -e c:/temp/IFInsertTest.log -o c:/temp/IFInsertTest_out.log
Why are you using -q option? I left it out.
According to BOL:
-q
Executes the SET QUOTED_IDENTIFIERS ON statement in the connection between the bcp utility and an instance of SQL Server. Use this option to specify a database, owner, table, or view name that contains a space or a quotation mark. Enclose the entire three-part table or view name in double quotation marks (" ").
SAMPLE CODE THAT WORKS:
--Create the sproc that generate the insert statment
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[IFGenerateInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[IFGenerateInsert]
GO
CREATE PROC IFGenerateInsert (
@TableName varchar(100)
)
AS
--generate an insert statement for testing
declare @insertStmt varchar(500)
set @insertStmt = 'insert into authors (id,name,book) values (1,''Joe Smith'',''Web Analytics'')'
select @insertStmt
GO
--Create the sproc that calls bcp via command shell
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[IFInsertTest]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[IFInsertTest]
GO
CREATE PROCEDURE IFINSERTTEST ( @FILEPATH NVARCHAR(50))
AS
DECLARE @cmd varchar(2000)
BEGIN
set @cmd = 'bcp.exe "EXEC cif.dbo.IFGenerateInsert ''LaborGroups''" '
+ 'QUERYOUT '+@filePath+ '.sql '
--update the userid and password
+'-S development -U XXX -P XXX -c -T -e '+ @filePath+'.log -o ' + @filePath+ '_out.log'
select @cmd -- + '...'
EXEC master.dbo.xp_cmdShell @cmd
END
GO
--Execute the Sproc
EXEC IFInsertTest 'c:/temp/IFInsertTest'
Hope this helps
July 19, 2007 at 3:02 pm
Thank you for your inputs
I will try it out, sp_generate_inserts code is from the following website.
July 19, 2007 at 3:29 pm
Just a side note here but in 2005 xp_cmdshell is disabled by default (Surface Area Config) and can also be a pretty dangerous thing to allow. Ever think about using SSIS for this stuff?
Ben Sullins
bensullins.com
Beer is my primary key...
July 19, 2007 at 3:45 pm
That link doesn't seem to work.
July 19, 2007 at 8:06 pm
Oh bother... all of us missed it ... see anything out of place here, folks???
alter PROCEDURE INSERTTEST2 ( @FILEPATH NVARCHAR(50))
AS
DECLARE @cmd varchar(2000)
BEGIN
set @cmd = 'bcp.exe "EXEC vbaicms_dev.dbo.SP_GENERATE_INSERTS auth" '
+ 'QUERYOUT' + ' ' +@filePath+ '.sql ' +'-S ' +
'NV-DEVSQL3\ASTRO' + ' -q ' + ' -c -T -e' + @filePath+'.log -o '
+ @filePath+ '_out.log'
select @cmd -- + '...'
EXEC master.dbo.xp_cmdShell @cmd
END
This proc should be like this... this isn't Oracle or PL/SQL... BEGIN/END are not required in procs but, if you want to use them, you can... but, unlike Oracle, variable declarations must be inside the first begin, no outside of PL/SQL blocks like in Oracle...
alter PROCEDURE INSERTTEST2 ( @FILEPATH NVARCHAR(50))
AS
BEGIN
DECLARE @cmd varchar(2000)
set @cmd = 'bcp.exe "EXEC vbaicms_dev.dbo.SP_GENERATE_INSERTS auth" '
+ 'QUERYOUT' + ' ' +@filePath+ '.sql ' +'-S ' +
'NV-DEVSQL3\ASTRO' + ' -q ' + ' -c -T -e' + @filePath+'.log -o '
+ @filePath+ '_out.log'
select @cmd -- + '...'
EXEC master.dbo.xp_cmdShell @cmd
END
--Jeff Moden
Change is inevitable... Change for the better is not.
July 20, 2007 at 7:14 am
Jeff, thank you for the reply but still doesnt seem to work. I get the same
error. I am running out of ideas
SQLState = 42000, NativeError = 536
Error = [Microsoft][SQL Native Client][SQL Server]Invalid length parameter passed to the SUBSTRING function.
Below is the cmd from PRINT statement.
bcp.exe "EXEC SP_GENERATE_INSERTS 'auth'" QUERYOUT C:\AICMS.sql -S NV-DEVSQL3\ASTRO -q -c -T -eC:\AICMS.log -o C:\AICMS_out.log
Thanks
July 20, 2007 at 9:58 am
For simplicity, did you try removing the Begin/End?
July 23, 2007 at 2:22 am
Try this:
Change "EXEC vbaicms_dev.dbo.SP_GENERATE_INSERTS auth"
to "EXEC dbo.SP_GENERATE_INSERTS auth"
.. and instead add the dbname with the -d parameter
(-d vbaicms_dev)
...and/or check if the proc SP_GENERATE_INSERTS is marked as a systemobject.
If it isn't, exec sp_MS_marksystemobject so that it is, then try again.
/Kenneth
July 23, 2007 at 3:56 am
Look for SUBSTRING in your mysterious SP_GENERATE_INSERTS procedure.
Find why CHARINDEX(...) inside SUBSTRING returns 0 and CHARINDEX(...)-1 becomes "Invalid length parameter".
_____________
Code for TallyGenerator
Viewing 15 posts - 1 through 15 (of 30 total)
You must be logged in to reply to this topic. Login to reply