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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Column Names

Status
Not open for further replies.

Waynest

Programmer
Jun 22, 2000
321
GB
While using a CREATE TABLE command I would like to use column names which have been determined within the same stored procedure (I want to set column names which represent a w/c date for display in a VB datagrid)

This is the kind of thing I mean...

-- I can determine date values from a calendar table
DECLARE @ColName char(8)
SELECT @ColName = '10/11/03'

DROP TABLE SkuPlan
CREATE TABLE SkuPlan
(
ItemCode char(15),
RecType char(1),
Description varchar(50),
@ColName int
)

This syntax isn't permitted, but any ideas how I could achieve the same effect?

Thanks
 
Hi there

look into dynamic sql, and have a read of FAQ183-3132

it would be something like
Code:
DECLARE @strSQL varchar(1000)
SET @strSQL = 
'CREATE TABLE SkuPlan (ItemCode char(15),RecType char(1),Description varchar(50), ' + @ColName + 'int)'

EXEC(@strSQL)

hope this helps
 
If you are using special chars in the name, enclose that part of the string with square brackets.

SET @strSQL =
'CREATE TABLE SkuPlan (ItemCode char(15),RecType char(1),Description varchar(50), [' + @ColName + '] int)'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top