473,382 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to make Database name a variable in a stored procedure?

The syntax to access a table is
Expand|Select|Wrap|Line Numbers
  1. Select   [DatabaseName].[Owner].[TableName] .[field] 
How do I make the DatabaseName a variable – ideally to be passed as a parameter into the stored procedure? The reason being my test database uses a different set of tables to the live database. Eg

Test database ‘EOMMarch2010Prod’ & ‘EOMMarch2010Log’
Live database ‘Production’ & ‘Logistics’

This stored procedure(shown below) is effectively “hardwired” to the test database, which means I have to change the database name once I have finished testing and want to deploy it on to the live database.
I ‘ve tried changing

Expand|Select|Wrap|Line Numbers
  1. (SELECT     TOP 100 PERCENT dbo.TraceSubAssy.PARENTJOBNUMBER AS Expr2 
  2.        FROM  EOMMarch2010Log.dbo.[vw Stock Tracability Summary by Lot]
  3. (SELECT     TOP 100 PERCENT dbo.TraceSubAssy.PARENTJOBNUMBER AS Expr2 
  4.        FROM  @DatabaseName.dbo.[vw Stock Tracability Summary by Lot]
But it doesnt like it. Any ideas?
The full script is below:-

Expand|Select|Wrap|Line Numbers
  1. USE [EOMMarch2010Prod]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[spPurchasesQueueFetch]    Script Date: 05/20/2010 14:30:47 ******/
  4. SET ANSI_NULLS ON
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7.  
  8.  
  9. -- =============================================
  10. -- Author:        Angela Andrews
  11. -- Create date: 20 May 20
  12. -- Description:    used in recall Database
  13. -- =============================================
  14. Create PROCEDURE [dbo].[spEndCustomerSummaryQueue]  @from_date datetime , @To_Date datetime,  @Kitnumber  NVARCHAR(20), @BatchLotNbr int, @DatabaseName varchar(50) , @Partnumber char(35) AS
  15.  
  16. begin
  17.     SET NOCOUNT ON;
  18.    select ParentJobNumber, SUM(QTYDESP) AS [SumQtyDesp], NavSellToCustomerNbr, Company_Ref, OriginalJobNumber      from  dbo.TraceAssyNo 
  19.    WHERE (NavSellToCustomerNbr <> '') AND (SuperceededByRMA = '') AND (Scrapped = 0 )
  20.  
  21.    GROUP BY ParentJobNumber, NavSellToCustomerNbr, Company_Ref, OriginalJobNumber 
  22.   HAVING      (ParentJobNumber IN 
  23.  
  24.   (SELECT     TOP 100 PERCENT dbo.TraceSubAssy.PARENTJOBNUMBER AS Expr2 
  25.        FROM  EOMMarch2010Log.dbo.[vw Stock Tracability Summary by Lot] INNER JOIN 
  26.                 dbo.traceKitNo ON EOMMarch2010Log.dbo.[vw Stock Tracability Summary by Lot].RELATEDKIT = dbo.traceKitNo.KITNUMBER INNER JOIN  
  27.                dbo.TraceSubAssy ON dbo.traceKitNo.SUBJOBNUMBER = dbo.TraceSubAssy.SUBJOBNUMBER 
  28.        GROUP BY  EOMMarch2010Log.dbo.[vw Stock Tracability Summary by Lot].PARTNUMBER, 
  29.              EOMMarch2010Log.dbo.[vw Stock Tracability Summary by Lot].SUPPLIERLOTNBR, dbo.TraceSubAssy.PARENTJOBNUMBER 
  30.        HAVING      (EOMMarch2010Log.dbo.[vw Stock Tracability Summary by Lot].PARTNUMBER = @Partnumber) AND  
  31.                      (  EOMMarch2010Log.dbo.[vw Stock Tracability Summary by Lot].SUPPLIERLOTNBR = @BatchLotNbr) )) 
  32.  
  33.  
  34. end
  35.  
  36.  
May 20 '10 #1
1 3765
Delerna
1,134 Expert 1GB
You would need to use dynamic sql for that. Check it in the help files for SQL server.

I would suggest that you don't pass the table name as a parameter though. You open yourself to SQL injection attacks.
It would become possible for someone to pass something like "DROP Table tableName" into your stored proc and then dynamic sql might execute it.


Better to pass a parameter for an
IF BEGIN END ELSE BEGIN END
test
You wouldn't need to use dynamic SQL at all then.

you could then say in the stored proc

Expand|Select|Wrap|Line Numbers
  1. IF @Param='TEST'
  2. BEGIN
  3.     --SQL to access the test database
  4. END ELSE BEGIN
  5.     --SQL to access the prod database
  6. END
  7.  
May 21 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Orlando Jin | last post by:
How can I get database SID in stored procedure in Oracle?
1
by: Robin Tucker | last post by:
Is it possible to execute a stored procedure in one database, which then itself executes a stored procedure from another database? We have decide to split our data into a tree structure (DB1) and...
4
by: Bruce | last post by:
I need to create a stored procedure in the master database that can access info to dynamically create a view in another database. It doesn't seem like it should be very hard, but I can't get it to...
6
by: Michael Spiegel | last post by:
Hi, I have a stored procedure that needs to know the name of the table from which it is called. If you are familiar with a "this" pointer in Java or C++, that's very similar to what I need. I...
3
by: Justin | last post by:
I am trying to do a simple update of a database using a stored procedure. I have get the following error when executing the code: "Procedure or function UpdateDetails has too many arguments...
3
by: jojo41300000 | last post by:
Hi, I am trying to pass the table name to stored procedure and then use the passing table name in select, update or delete query statement. When I tried to do that, I got the error such as...
1
by: ishakteyran | last post by:
hi all; i am using a db2 database on RAD platform and need to generate tables (names of them will be determined by users.) so i thought i can do this with a stored procedure which takes the table...
0
by: ssouhrada | last post by:
I am trying to create a stored procedure that runs a smaller query first to determine the database to run the second query on. Below is a sample query that is similar to what I would be doing. I'm...
1
by: barmatt80 | last post by:
I am in need of calling a DB2 stored procedure from MS SQL server 2005. If this is all possible. The ultimate goal is to have a user open the vb.net program, enter the employee ssn, and it will...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.