April 19, 2005 at 12:25 pm
Thank you for your help. I need to recreate a lot of stored procs to a different server and database, I was thinking of modifying my store procs by adding a variable to call a specific database, can this be done? If so, how? My stored procs looks like this:
CREATE PROCEDURE usp_ETLCommission_Plan
AS
-- Replacing with query which brings binary id from table where binary id is the key as opposed to straight
--from the extracted and transformed construction stage table.
--SELECT
-- *
--FROM
-- ETLBD_Commission_Plan
SELECT
BCP.BD_Begin_Date,
BCP.BD_Default_Plan,
BCP.BD_End_Date,
BCP.BD_Plan_Name,
BCP.BD_Plan_Type,
BCP.ETLBD_Commission_Plan_Id,
ED.PivotalDivision_Id AS BD_Project_Id
FROM
PivotalTLStaging.dbo.ETLBD_Commission_Plan BCP WITH (NOLOCK)
INNER JOIN PivotalTLStaging.dbo.ETLDivision ED WITH (NOLOCK)
ON BCP.ETLBD_Project_Id = ED.ETLDivisionID
GO
Where the call to the database is being done here PivotalTLStaging.dbo.ETLBD_Commission_Plan and send the record set to a different database
Thank you for your assistance.
April 19, 2005 at 1:26 pm
Will this help?
DECLARE @stmt NVARCHAR(1000)
SET @stmt = 'USE NORTHWIND' + CHAR(10)+'SELECT * FROM Orders'
EXEC sp_executeSQL @stmt
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 20, 2005 at 4:10 am
Thanks Frank, I will try. What I don't is to hard code the database name to allow any of my stored procedures to run regardless of the database to be used. I think your approach could help because the change will be only in one section of the store proc. I will give it a try. Thanks again
April 20, 2005 at 5:45 am
Why don't you just leave the dbname out?
There's no point, and not preferred either, to reference tables with dbname.owner.objectname if it's not intended to be a crossdatabase query in the first place.
In your example, just change
PivotalTLStaging.dbo.ETLBD_Commission_Plan BCP WITH (NOLOCK)
INNER JOIN PivotalTLStaging.dbo.ETLDivision ED WITH (NOLOCK)
to
dbo.ETLBD_Commission_Plan BCP WITH (NOLOCK)
INNER JOIN dbo.ETLDivision ED WITH (NOLOCK)
and you're done...
/Kenneth
April 20, 2005 at 5:48 am
Doh, I thought he was after dynamically switching db context...
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 20, 2005 at 6:28 am
Well... re-reading the original post (last paragraph) I realize that I'm not entirely sure either what the real question is about....
I'm probably lost as well..
/Kenneth
April 20, 2005 at 12:03 pm
Sorry guys for not being clear enough. What happen is that the stored procs are use in a data base, but insert data into a different one. That is why I thought of getting a db variable and run them from that database or any other and not restric myself to the same database and change the database name each environment. We have three diffrent servers and the develores use different names for the DBs (why? please don't ask!!! LOL) SO, I ended up creating the stored procs for the first environment without realizing those three other environments. That is the reason behind this, other wise I will just create the stored procs in one database and that will do it. Dinamically run them all.
I think Frank's solution is going to work. Not sure yet, but it worked for a couple!! Cool!!
April 21, 2005 at 2:52 am
Another solution is to use a led pipe and apply it to the devs fingers.
If they can't begin being consistent with names even, how can you then trust the stuff they actually 'develop'..?
well... no offense to devs, really - but what is a fact, (imo anyway) is that it's way to much code out there with the sole purpose of 'working around bad/lazy habits'
/Kenneth
April 21, 2005 at 5:39 am
You are absolutely correct, no disrespect but sometimes .... Enough of that. It seems, so far, that the solution given by Frank worked for them. I talk to one of the devs and he said, that it could be the solution for all their problems. I hope it does and I hope there is no more headaches!! Fat chance, but dream doesn't cost a cent!! Thanks for all your help.
April 21, 2005 at 6:42 am
To be honest, when devs say that a dynamic solution is the 'solution for all their problems' I literally get chills down my spine.
Before you let them play with this any further, please read up on what, why and how this really works... and what the cost you're about to pay for it may be..
http://www.sommarskog.se/ (especially the one titled 'The Curse and Blessings of Dynamic SQL')
/Kenneth
April 21, 2005 at 7:49 am
Thanks, I know, I know and that's exactly what I am doing today!!
April 21, 2005 at 9:02 am
There is another way to solve this!
Declare @dbname1 varchar(20), @dbname2 varchar(20), @spname varchar(128), @retval int
select @dbname1 = 'FirstDBNAME', @dbname2 ='SECONDDBNAME'
-- call proc on db1
set @spname = @dbname1 +'.dbo.procedurename'
exec @retval = @spname
-- call proc on db2
set @spname = @dbname2 +'.dbo.procedurename'
exec @retval = @spname
You could even pass parameter like
-- exec @retval = @spname, @parm1, @param2
hth
* Noel
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply