Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CREATE TABLE using @Variables?

Status
Not open for further replies.

bind

IS-IT--Management
Dec 13, 2004
25
Is it possible to use CREATE TABLE using a variable ?

Heres my example source however obviously its not functioning properly.

DECLARE @CurrDate AS DATETIME

SET @CurrDate = (SELECT DATEADD(Year, 1, GETDATE()))

CREATE TABLE @CurrDate
(NASIdentifier varchar(16),
NASPort varchar(10),
AcctSessionID varchar(10),
AcctStatusType tinyint(1),
CallDate smalldatetime(4),
UserName varchar(32),
UserService tinyint(1),
AcctDelayTime int(4),
AcctSessionTime int(4),
FramedProtocol int(4),
FramedAddress varchar(16),
AcctInputOctets int(4),
AcctOutputOctets int(4),
AcctTerminateCause tinyint(1),
NASPortType tinyint(1),
NASPortDNIS varchar(10),
CallerID varchar(12),
ConnectInfo varchar(32)
)
 
You are declaring your variable as Datetime, but you are trying to create a recordset object, not a date.
 
Not good practice to do this, but it is possible using
Code:
declare @v_SQL varchar(2000)
DECLARE @CurrDate AS DATETIME

SELECT @CurrDate = DATEADD(Year, 1, GETDATE())
SET @v_SQL = 'CREATE TABLE ' + convert(varchar(21), DATEADD(Year, 1, GETDATE())) + ' 
(NASIdentifier varchar(16),
NASPort varchar(10),
AcctSessionID varchar(10),
AcctStatusType tinyint(1),
CallDate smalldatetime(4),
UserName varchar(32),
UserService tinyint(1),
AcctDelayTime int(4),
AcctSessionTime int(4),
FramedProtocol int(4),
FramedAddress varchar(16),
AcctInputOctets int(4),
AcctOutputOctets int(4),
AcctTerminateCause tinyint(1),
NASPortType tinyint(1),
NASPortDNIS varchar(10),
CallerID varchar(12),
ConnectInfo varchar(32)
) '

exec (@v_sql)


"I'm living so far beyond my income that we may almost be said to be living apart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top