Solved

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

Posted on 2008-10-10
5
733 Views
Last Modified: 2012-05-05
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
Comment
Question by:chrobi
  • 3
5 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22690918
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
 
LVL 1

Author Comment

by:chrobi
ID: 22691062
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
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22691565
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
 
LVL 1

Author Comment

by:chrobi
ID: 22691957
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
 
LVL 1

Accepted Solution

by:
chrobi earned 0 total points
ID: 22746969
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now