473,386 Members | 1,715 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,386 software developers and data experts.

giving a proc database and table names as parameters

Hello!

Is it possible to pass a stored procedure a parameter, say @table and
use it as a table in the sql command?
Finally i want a proc to copy tables from a database to another database.

THERE IS MY CODE:

CREATE PROCEDURE [user].[copytable]
@dbSRC varchar(100),
@dbTRGT varchar(100),
@table varchar(100)
AS

BEGIN TRANSACTION FreeAndCopyTable
TRUNCATE TABLE [@dbTRGT].admin.[@table]
INSERT INTO [@dbTRGT].admin.[@table]
SELECT *
FROM [@dbSRC].admin.[@table]

COMMIT TRANSACTION FreeAndCopyTable
IF @@error <0 GOTO E_Copy_Fail

E_Copy_Fail:
ROLLBACK TRANSACTION FreeAndCopyTable

GO

thanks in advance,
muh
Jan 10 '07 #1
2 1492
Dynamic SQL:
Books Online: Using sp_executesql
Web: http://www.sommarskog.se/dynamic_sql.html

Muhammed Al-Khoutani wrote:
Hello!

Is it possible to pass a stored procedure a parameter, say @table and
use it as a table in the sql command?
Finally i want a proc to copy tables from a database to another database.

THERE IS MY CODE:

CREATE PROCEDURE [user].[copytable]
@dbSRC varchar(100),
@dbTRGT varchar(100),
@table varchar(100)
AS

BEGIN TRANSACTION FreeAndCopyTable
TRUNCATE TABLE [@dbTRGT].admin.[@table]
INSERT INTO [@dbTRGT].admin.[@table]
SELECT *
FROM [@dbSRC].admin.[@table]

COMMIT TRANSACTION FreeAndCopyTable
IF @@error <0 GOTO E_Copy_Fail

E_Copy_Fail:
ROLLBACK TRANSACTION FreeAndCopyTable

GO

thanks in advance,
muh
Jan 10 '07 #2
Thanks!
Everything is clear now :)

Steve schrieb:
Dynamic SQL:
Books Online: Using sp_executesql
Web: http://www.sommarskog.se/dynamic_sql.html

Muhammed Al-Khoutani wrote:
>Hello!

Is it possible to pass a stored procedure a parameter, say @table and
use it as a table in the sql command?
Finally i want a proc to copy tables from a database to another database.

THERE IS MY CODE:

CREATE PROCEDURE [user].[copytable]
@dbSRC varchar(100),
@dbTRGT varchar(100),
@table varchar(100)
AS

BEGIN TRANSACTION FreeAndCopyTable
TRUNCATE TABLE [@dbTRGT].admin.[@table]
INSERT INTO [@dbTRGT].admin.[@table]
SELECT *
FROM [@dbSRC].admin.[@table]

COMMIT TRANSACTION FreeAndCopyTable
IF @@error <0 GOTO E_Copy_Fail

E_Copy_Fail:
ROLLBACK TRANSACTION FreeAndCopyTable

GO

thanks in advance,
muh
Jan 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
5
by: dotyet | last post by:
I have been given the daunting task of sql query tuning. I am looking for ways to get started with that. I am on DB2 UDB 8.2 (8.1 with Fixpak 8) on Windows. One point which I could think about...
4
by: CSDunn | last post by:
Hello, I have a combo box (Combo7) that needs to call a function during the After Update event of the combo box. The function resides in an Access 2000 ADP Module called MMAnswerData_code. The...
6
by: David Lozzi | last post by:
Here is the proc: CREATE PROCEDURE . @CID as int, @Netname as nvarchar(25), @Return as int OUTPUT AS IF EXISTS (SELECT DISTINCT netname FROM computers WHERE CompanyID = @CID AND...
6
by: Marcel Hug | last post by:
Hi all ! I have a table in my database, which has 3 attributes. IDFailureControl, ControlDate and ControlVersion. In the following function I test, if the date of today allready exists. Then I...
1
by: Phil Mc | last post by:
Trying to call a stored proc but some times don't want to have values inserted in some fields. Hi I am rewriting a VBS script which called a stored proc in a SQL server db. The proc takes a...
6
by: Paul M | last post by:
Hi All, I'm currently writing a z/OS DB2 Stored Proc in C, using an example from the IBM Stored Procedure guide (SG24-7083-00). The database calls to read and update the database work...
2
by: Rob Eventine | last post by:
hi all, how does one go about changing the details in the aspnetdb dbase using the supplied stored procs? i am using vb as my language but i have no idea how to use them, how to call them or...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.