Link to home
Start Free TrialLog in
Avatar of chrobi
chrobiFlag for United States of America

asked on

Create stored Procedure to Create another Stored Procedure in another database.

Hello All,

I am trying to create a stored procedure that will create another stored procedure in another database.  Every time I try to use the stored proc I get the error that the "Identifier used .... is too long 128 is the limit."

I have to have dynamic sql in the new stored procedure cause the program accessing the stored proc doesn't know which table to put the data in until run time.

Any help would be greatly appreciated...
SET @THISISIT = '[' + @CustomerID +'].sys.sp_executesql( '
 
SET @THISISIT1 = '"CREATE PROCEDURE [dbo].[sp_insert_posdata] 
@idx bigint,
@Table nvarchar(100),
@Receipt_No nvarchar(20), 
@Register_Time DateTime, 
@Server_Time DateTime, 
@Item nvarchar(50), 
@value_Char nvarchar(20), 
@value_Num money, 
@Sequence_Num int, 
@Register_Num int, 
@dvr_id int,
@terminal_num int,
@void bit,
@nosale bit,
@quantity int,
@cashier nvarchar(50)
AS 
DECLARE @Insert varchar(255)
DECLARE @Insert2 varchar(255)
SET @Insert = INSERT INTO dbo. + @Table +
(idx, receipt_no, register_time, server_time, item, value_char, value_num, sequence_num,
SET @Insert2 =register_num, dvr_id, terminal_num, void, nosale, quantity, cashier)
VALUES
(@idx, @Receipt_No, @Register_Time, @Server_Time, @Item, @Value_Char, @Value_Num, @Sequence_Num, @Register_Num, @dvr_id, @terminal_num, @void, @nosale, @quantity, @cashier)"
exec(@Insert +@Insert2)'
 
exec( @THISISIT +  @THISISIT1)

Open in new window

Avatar of BrandonGalderisi
BrandonGalderisi
Flag of United States of America image

You are encapsulating your quoted data in double quotes:

But there are other problems with this.

You are trying build SQL in the target procedure by setting @Insert, but your variables won't exist in the context of that without building ANOTHER sp_ExecuteSQL and passing in parameters.  But I have to ask you, why not just create the procedure on the remote server?  Why write SQL in a dynamic exec to do it?

SET @THISISIT1 = 'CREATE PROCEDURE [dbo].[sp_insert_posdata] 
@idx bigint,
@Table nvarchar(100),
@Receipt_No nvarchar(20), 
@Register_Time DateTime, 
@Server_Time DateTime, 
@Item nvarchar(50), 
@value_Char nvarchar(20), 
@value_Num money, 
@Sequence_Num int, 
@Register_Num int, 
@dvr_id int,
@terminal_num int,
@void bit,
@nosale bit,
@quantity int,
@cashier nvarchar(50)
AS 
DECLARE @Insert varchar(255)
DECLARE @Insert2 varchar(255)
SET @Insert = ''INSERT INTO dbo.'' + @Table + ''
(idx, receipt_no, register_time, server_time, item, value_char, value_num, sequence_num,
SET @Insert2 =register_num, dvr_id, terminal_num, void, nosale, quantity, cashier)
VALUES
(@idx, @Receipt_No, @Register_Time, @Server_Time, @Item, @Value_Char, @Value_Num, @Sequence_Num, @Register_Num, @dvr_id, @terminal_num, @void, @nosale, @quantity, @cashier)
exec(@Insert +@Insert2)'
 
exec( @THISISIT +  @THISISIT1)

Open in new window

Avatar of chrobi

ASKER

Thank you for the quick respond to my question Brandon.

After reading your code, I see that its adding to the current database the stored proc is running from.  I need it to insert into the new database that I just created.  

Order of how I am thinking this should work:

SP is called with Proper info > SP Creates the new Database > Creates New User> Associate new user to new database >Creates Dynamic Stored Proc in new database.

I have everything else working execept the "Creates Dynamic Stored Proc"

And that is what i am trying to accomplish.  The new Database that is created has a "@NDB sysname" with the database name in it.

Am I barking up the wrong tree trying to accomplish it like this?
If you look at MSDN, you can see that SP can only be created in current database.
To workarround that, i suggest that you create a SP in the other database, passing your stored procedure create script, to execute it.
Database1 (where you want to create the SP)
CREATE PROCEDURE sp_create(@sql varchar(MAX))
AS
exec @sql
Database2 (where you call the SP to create the other SP in Database1)
put your code and call the other SP using
EXEC Database1.sp_create 'your SP create script'
Avatar of chrobi

ASKER

Thank you for the information...  I would like to pose a different thought process to accomplish this task.  I need it to be fully automated, because I am not going to have time make sure the sp_create sp is in each of the databases.  What if I was to script this all out in sql file and have my run the script file to do the
" SP is called with Proper info > SP Creates the new Database > Creates New User> Associate new user to new database >Creates Dynamic Stored Proc in new database."   Is it possible to pass variables in to the script?  I truelly appreciate your time on this issue.


ASKER CERTIFIED SOLUTION
Avatar of chrobi
chrobi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial