all groups > sql server dts > august 2005 >
You're in the

sql server dts

group:

DTS Global variable in an Insert?


DTS Global variable in an Insert? Sharon McMillon
8/18/2005 3:42:19 PM
sql server dts:
Is there a way to do the following in a dts package?
Using the pubs db -
INSERT
INTO db1.dbo.authors
(au_id, au_fname, au_lname, city, state, zip, phone, contract)
SELECT au_id, @FIRSTNAME, @LASTNAME, city, state, zip, phone,
contract)
FROM
db2.dbo.authors


Where @FIRSTNAME and @LASTNAME are global variables that I can call in
additional tasks? The values are static, and I have just been
declaring them in each of my tasks, but That means I need to update
this static value in each task when we go live.
I'd like to make it global so I only have to change it once.


Basically, I'm migrating a ton of data from one system to another, and
the destination system has 6 static fields that are in EVERY table.
So, I have to declare these for EVERY Insert.
Re: DTS Global variable in an Insert? Sharon McMillon
8/18/2005 4:13:38 PM
I can successfully use the placeholder '?' in a where clause, but when
I use it in the FROM part, I get a syntax error.
Re: DTS Global variable in an Insert? Sharon McMillon
8/18/2005 4:15:03 PM
whoops - sorry - when I use it in the SELECT, I get a syntax error.
Re: DTS Global variable in an Insert? Tom Moreau
8/18/2005 6:54:36 PM
Yep. Proceed as follows:

1) Create an ExecuteSQL task, using your SQL Server connection.
2) Make the code as follows:

INSERT
INTO db1.dbo.authors
(au_id, au_fname, au_lname, city, state, zip, phone, contract)
SELECT au_id, ?, ?, city, state, zip, phone,
contract)
FROM
db2.dbo.authors

3) Assign the first parm to the global for first name. (use the
Parameters button)
4) Assign the second parm to the global for last name. (same here)

Needless to say, be sure to set the values for the globals.

HTH

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
[quoted text, click to view]
Is there a way to do the following in a dts package?
Using the pubs db -
INSERT
INTO db1.dbo.authors
(au_id, au_fname, au_lname, city, state, zip, phone, contract)
SELECT au_id, @FIRSTNAME, @LASTNAME, city, state, zip, phone,
contract)
FROM
db2.dbo.authors


Where @FIRSTNAME and @LASTNAME are global variables that I can call in
additional tasks? The values are static, and I have just been
declaring them in each of my tasks, but That means I need to update
this static value in each task when we go live.
I'd like to make it global so I only have to change it once.


Basically, I'm migrating a ton of data from one system to another, and
the destination system has 6 static fields that are in EVERY table.
So, I have to declare these for EVERY Insert.
Re: DTS Global variable in an Insert? Tom Moreau
8/18/2005 8:21:11 PM
How about showing us the exact SQL code?

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
[quoted text, click to view]
whoops - sorry - when I use it in the SELECT, I get a syntax error.
Re: DTS Global variable in an Insert? Allan Mitchell
8/18/2005 11:55:19 PM
What about

INSERT
INTO db1.dbo.authors (au_id, au_fname, au_lname, city, state, zip, phone,
contract)
SELECT au_id, ?, ? city, state, zip, phone,contract)
FROM
db2.dbo.authors

You can then map a Global variable onto the ? using the Parameters button.




[quoted text, click to view]

Re: DTS Global variable in an Insert? Darren Green
8/18/2005 11:55:55 PM
To reference a global variable in an Exec SQL Task, use the placeholder ?.

e.g.

SELECT * FROM T WHERE x = ?

Then click Parameters, to map the parameters detected to global variables.

--
Darren Green
http://www.sqldts.com
Re: DTS Global variable in an Insert? Darren Green
8/19/2005 12:00:00 AM
ActiveX Script Task based dynamic SQL generally works best for this-

Global Variables and SQL statements in DTS
(http://www.sqldts.com/default.aspx?205)

[quoted text, click to view]

Re: DTS Global variable in an Insert? Andy
8/19/2005 5:49:32 AM
If the table name is going to be dynamice, you cannot pass a variable into
the from statement. To do this, unfortunately you have to use dynamic SQL.

[quoted text, click to view]
Re: DTS Global variable in an Insert? Sharon McMillon
8/19/2005 8:49:27 AM
The exact code is the same as above:

INSERT
INTO db1.dbo.Authors
(au_id, au_fname, au_lname, city, state, zip, phone, contract)
SELECT au_id, ?, ?, city, state, zip, phone, contract
FROM db2.dbo.Authors

This gives me a syntax error. If I remove the '?' place holders, it
works fine.
Re: DTS Global variable in an Insert? Tom Moreau
8/19/2005 8:57:31 PM
Put the SQL into a stored proc with parameters:

create proc InsertTom
(
@lname varchar (20)
, @fname varchar (20)
)
as
set nocount on

INSERT
INTO db1.dbo.Authors
(au_id, au_fname, au_lname, city, state, zip, phone, contract)
SELECT au_id, @lname, @fname, city, state, zip, phone, contract
FROM db2.dbo.Authors

go


Then, change your SQL to:

exec InsertTom ?, ?

Then, assign the parameters.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
[quoted text, click to view]
The exact code is the same as above:

INSERT
INTO db1.dbo.Authors
(au_id, au_fname, au_lname, city, state, zip, phone, contract)
SELECT au_id, ?, ?, city, state, zip, phone, contract
FROM db2.dbo.Authors

This gives me a syntax error. If I remove the '?' place holders, it
works fine.
AddThis Social Bookmark Button