Solved

How do I use a variable for a Default vlaue in a SQL Create Table statement?

Posted on 2010-09-15
7
1,149 Views
Last Modified: 2012-05-10
I'm trying to modify a script to use a variable (@ParamID) as a Default value for a column in my Create Table statement, however when I execute the script below, I get an error "Variables are not allowed in the CREATE TABLE statement."  Can this be done?  Could somebody give me a sample please?  Any way to use the value directly from the Select statement without using the variable?

The SQL script:

DECLARE @ParamID AS int
SET @ParamID = (SELECT Shift FROM DatabaseVersion WHERE ObjectID = 1)
...
CREATE TABLE [dbo].[IntercomProtocol](
...
[PartitionID] [dbo].[PartitionReference] DEFAULT (@ParamID) NOT NULL,
...
0
Comment
Question by:Keith3679
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:dantheanswerman
ID: 33686495
SELECT @ParamID =Shift FROM DatabaseVersion WHERE ObjectID = 1)

declare @SQL varchar(max)
set @sql = 'CREATE TABLE [dbo].[IntercomProtocol](
...
[PartitionID] [dbo].[PartitionReference] DEFAULT (''' + @ParamID + ''') NOT NULL,

'
print @SQL
exec (@SQL)


That will do the trick. ;)
0
 
LVL 2

Expert Comment

by:dantheanswerman
ID: 33686536
SELECT @ParamID =Shift FROM DatabaseVersion WHERE ObjectID = 1)

declare @SQL varchar(max)
set @sql = 'CREATE TABLE [dbo].[IntercomProtocol](
...
[PartitionReference] int DEFAULT (' + @ParamID + ') NOT NULL,

'
print @SQL
exec (@SQL)

-- Few changes -- you only add the field name in the table creation script, as as you're probably using an integer for the type for column [PartitionReference], you do not need the extra quotes around the @ParamID -- only one single quote on either side of the +
0
 

Author Comment

by:Keith3679
ID: 33686758
What is the "set @sql = 'CREATE TABLE..." for?  Why do you need this?  Is the CREATE TABLE returning a value?
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 2

Accepted Solution

by:
dantheanswerman earned 500 total points
ID: 33686892
Nope.. but you can't have variable in a table creation statement unless you script it... it's a string of text that you execute to create the table.

so you wrap your entire table create statement in quotes and execute it in a batch with the "exec (@SQL)" command.

The quotes can be tricky... if you like, you can post your entire create statement, I'll show you how it's done.
0
 
LVL 7

Expert Comment

by:Anoo S Pillai
ID: 33687704
Answer to "What is the "set @sql = 'CREATE TABLE..." for? :- " 

While using this trick you are creating a string which is exactly the command to be run on SQL server.  ( You are opting this technique as you do not know the default value , and hence NOT the exact  table create command until runtime) . Strings which are valid SQL commands can be executed useing EXEC () or sp_execteSQL.  

Search for Dynamic queries for more details.
0
 

Author Comment

by:Keith3679
ID: 33687950
Ahh, it's a string--I didn't see the other quote!  It makes perfect sense.  Thanks for the info, I will try this tomorrow and let you know how it goes.
0
 

Author Closing Comment

by:Keith3679
ID: 33697460
The general idea demonstrated was the solution.  The sample partially worked.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question