• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

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

0
chrobi
Asked:
chrobi
  • 3
1 Solution
 
BrandonGalderisiCommented:
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

0
 
chrobiAuthor Commented:
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?
0
 
tiagosalgadoCommented:
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'
0
 
chrobiAuthor Commented:
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.


0
 
chrobiAuthor Commented:
I ended up re-writing the process to have batch file do the insert of the stored procedure using exec xp_CMDShell.  If any one uses this as their fix.. I strongly suggest that you review the downsides to using this, from my reading you have to make sure to take necessary precautions to ensure you prevent injection of malicious code.  

Thanks for the responses to my questions, but being that I found out a solution to my issue, I will not be giving the points for this question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now