Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-09-15
7
Medium Priority
?
1,190 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 2

Accepted Solution

by:
dantheanswerman earned 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

610 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