dbTalk Databases Forums  

variables in execute SQL task

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss variables in execute SQL task in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SQLCat
 
Posts: n/a

Default variables in execute SQL task - 10-04-2006 , 08:42 AM






I have a DTS job step with the SQL below (abridged). As an example, I want
to take the file name 'mms_datamodules_Oct_Data' and dynamically assign it to
the current month instead of hard coding it each month. How can variables be
used to accomplish this?

CREATE DATABASE [mms_datamodules_new] ON (NAME =
N'mms_datamodules_Oct_Data',
FILENAME = N'D:\Program Files\Microsoft SQL
Server\MSSQL\data\mms_datamodules_Oct_Data.MDF' ,
SIZE = 2500, FILEGROWTH = 10%) LOG ON (NAME = N'mms_datamodules_Oct_Log',
FILENAME = N'D:\Program Files\Microsoft SQL
Server\MSSQL\data\mms_datamodules_Oct_Log.LDF' ,
SIZE = 13, FILEGROWTH = 10%)
COLLATE SQL_Latin1_General_CP1_CI_AS
GO


Reply With Quote
  #2  
Old   
Mescha
 
Posts: n/a

Default RE: variables in execute SQL task - 10-10-2006 , 09:24 AM






You would need to use dynamic sql to create a string with your sql statement
in it. You can then use sp_executesql to fire off the query. Of course you
would need to also setup a variable and set it via some sql to get the month
you want to create.
--
Keith Mescha


"SQLCat" wrote:

Quote:
I have a DTS job step with the SQL below (abridged). As an example, I want
to take the file name 'mms_datamodules_Oct_Data' and dynamically assign it to
the current month instead of hard coding it each month. How can variables be
used to accomplish this?

CREATE DATABASE [mms_datamodules_new] ON (NAME =
N'mms_datamodules_Oct_Data',
FILENAME = N'D:\Program Files\Microsoft SQL
Server\MSSQL\data\mms_datamodules_Oct_Data.MDF' ,
SIZE = 2500, FILEGROWTH = 10%) LOG ON (NAME = N'mms_datamodules_Oct_Log',
FILENAME = N'D:\Program Files\Microsoft SQL
Server\MSSQL\data\mms_datamodules_Oct_Log.LDF' ,
SIZE = 13, FILEGROWTH = 10%)
COLLATE SQL_Latin1_General_CP1_CI_AS
GO


Reply With Quote
  #3  
Old   
SQLCat
 
Posts: n/a

Default RE: variables in execute SQL task - 10-10-2006 , 10:30 AM



I ended up using monthname as a variable and assisning the sql datename(m,
getdate()) to it as displayed below: (I could also use the sp_executesql proc
as well)

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name =
N'mms_datamodules_new')
DROP DATABASE [mms_datamodules_new]
GO

Declare @monthname varchar (15)
Set @monthname = datename(m, getdate())
Declare @strSQL varchar(8000)

SET @strSQL = 'CREATE DATABASE mms_datamodules_new
ON (NAME = mms_datamodules_'+@monthname+'_Data,
FILENAME = [D:\Program Files\Microsoft Sql
Server\MSSQL\Data\mms_datamodules_'+@monthname+'_D ata.MDF] ,
SIZE = 2500, FILEGROWTH = 10%) LOG ON (NAME =
mms_datamodules_'+@monthname+'_Log,
FILENAME = [D:\Program Files\Microsoft Sql
Server\MSSQL\Data\mms_datamodules_'+@monthname+'_L og.LDF] ,
SIZE = 13,
FILEGROWTH = 10%)'

EXEC (@strSQL)

"Mescha" wrote:

Quote:
You would need to use dynamic sql to create a string with your sql statement
in it. You can then use sp_executesql to fire off the query. Of course you
would need to also setup a variable and set it via some sql to get the month
you want to create.
--
Keith Mescha


"SQLCat" wrote:

I have a DTS job step with the SQL below (abridged). As an example, I want
to take the file name 'mms_datamodules_Oct_Data' and dynamically assign it to
the current month instead of hard coding it each month. How can variables be
used to accomplish this?

CREATE DATABASE [mms_datamodules_new] ON (NAME =
N'mms_datamodules_Oct_Data',
FILENAME = N'D:\Program Files\Microsoft SQL
Server\MSSQL\data\mms_datamodules_Oct_Data.MDF' ,
SIZE = 2500, FILEGROWTH = 10%) LOG ON (NAME = N'mms_datamodules_Oct_Log',
FILENAME = N'D:\Program Files\Microsoft SQL
Server\MSSQL\data\mms_datamodules_Oct_Log.LDF' ,
SIZE = 13, FILEGROWTH = 10%)
COLLATE SQL_Latin1_General_CP1_CI_AS
GO


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.