Solved

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

Posted on 2010-09-15
7
1,139 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
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

840 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