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

To cut a very long story short I wa

Status
Not open for further replies.

mikehilly

IS-IT--Management
Jun 2, 2000
3
HK
To cut a very long story short I want to use varibles in an Alter command in an SP i.e
ALTER TABLE @tableName ALTER COLUMN @fieldName datetime
but it gives me a syntax error. Is there any way around it or should I start hand cranking database mods now.

Regards

Mike
 
You want to use what is called dynamic sql
i.e. something like this

DECLARE @Table VARCHAR(100)
DECLARE @Column VARCHAR(100)
DECLARE @SQL VARCHAR(8000)

SET @Table = 'mytable'
SET @Column = 'mycolumn'

SET @SQL = 'Alter table ' + @Table + ' ALTER COLUMN ' + @Column + ' datetime'

PRINT @SQL
EXEC(@SQL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top