Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2004, 01:06 PM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert using variables

Hello all.

I'm inserting data from multiple databases on one server into a single database and I'd like to be able to generate the tables names I'm inserting to and selecting from in variables. I have 4 separate environments that these scripts need to run in and I'd like to avoid having to alter them every time they move to another environment.

Here's a simple example of what I'm trying to do:

DECLARE @Source varchar(50),
        @Target varchar(50)

select @Target = '[' + table_catalog + ']' + '.' + '[' + table_schema + ']' + '.' + 'Charge'
                from Information_Schema.tables where table_name = 'Charge'


select @Source = '[' + table_catalog + ']' + '.' + '[' + table_schema + ']' + '.' + 'Charge'
                from [asapsql].Information_Schema.tables where table_name = 'Charge'

INSERT INTO [@Target]
        ( [ASAP_Charge_ID]
        , [Charge_Description]
        , [Update_User_ID]
        , [Update_DateTime])
SELECT [Charge_ID]
        , [Charge_Description]
        , [Update_User_ID]
        , [Update_DateTime]
FROM [@Source]

Using the variables in the insert statement yields and "Invalid object name '@Target'." error.

Any suggestions for generating the table names for use in the insert statement?

Thanks!!
 
Old November 1st, 2004, 05:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

You got to construct those insert statements into separate variables and use EXEC() function to execute that, as these are dynamic sql statements. Something that looks like this
Code:
Select @strSql = 'INSERT INTO [' + @Target + ']
        ( [ASAP_Charge_ID]
        , [Charge_Description]
        , [Update_User_ID]
        , [Update_DateTime])
   SELECT [Charge_ID]
        , [Charge_Description]
        , [Update_User_ID]
        , [Update_DateTime]
     FROM [' + @Source + ']'
Exec(@strSql)
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old November 1st, 2004, 07:13 PM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It does help - I was hoping there was another method, but I guess it will have to do.

Thanks!

Quote:
quote:Originally posted by happygv
 Hi,

You got to construct those insert statements into separate variables and use EXEC() function to execute that, as these are dynamic sql statements. Something that looks like this
Code:
Select @strSql = 'INSERT INTO [' + @Target + ']
        ( [ASAP_Charge_ID]
        , [Charge_Description]
        , [Update_User_ID]
        , [Update_DateTime])
   SELECT [Charge_ID]
        , [Charge_Description]
        , [Update_User_ID]
        , [Update_DateTime]
     FROM [' + @Source + ']'
Exec(@strSql)
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
variables daniel.mihalcea Javascript 1 September 9th, 2008 07:06 PM
using variables webgrphx Classic ASP Basics 3 April 11th, 2007 06:46 PM
Defining the Variables. dpkbahuguna Beginning VB 6 2 November 1st, 2006 12:40 PM
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.