Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
Create database and schema dynamically
Create database and schema dynamically
Rate Topic
Display Mode
Topic Options
Author
Message
Robbert Hof
Robbert Hof
Posted Wednesday, November 28, 2012 12:22 AM
Ten Centuries
Group: General Forum Members
Last Login: Yesterday @ 4:29 AM
Points: 1,282,
Visits: 744
I want to make a Stored Procedure to create a database with a dynamic name.
In that SP I have to create a new schema in that new database as well.
create proc CREATE_LOAD_DB @load_year int, @load_month int
as
declare @dbname sysname
declare @sql nvarchar(max)
set @dbname = 'DB_' + CAST(@load_year as varchar(4)) + CAST(@load_month as varchar(2))
set @sql = 'create database ' + QUOTENAME(@dbname)
exec (@sql)
How to create a schema in this new database within the SP?
thanks,
Robbert
Post #1389532
Koen Verbeeck
Koen Verbeeck
Posted Wednesday, November 28, 2012 12:29 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Today @ 6:51 AM
Points: 9,373,
Visits: 6,470
I don't think you can.
You can't use the "USE database" statement in a stored procedure and you cannot qualify the schema with a database name in the CREATE SCHEMA statement.
How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
Member of
LinkedIn
. My blog at
LessThanDot
.
MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1389538
WolfgangE
WolfgangE
Posted Wednesday, November 28, 2012 1:15 AM
Valued Member
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
It should work if you include the USE-Statement in the dynamic SQL-String for creating the schema.
set @sql = 'use ' + QUOTENAME(@dbname) + ' create schema MyNewSchema';
exec ( @sql )
Post #1389553
Robbert Hof
Robbert Hof
Posted Wednesday, November 28, 2012 1:24 AM
Ten Centuries
Group: General Forum Members
Last Login: Yesterday @ 4:29 AM
Points: 1,282,
Visits: 744
I tried that, giving this error:
Msg 111, Level 15, State 1, Line 1
'CREATE SCHEMA' must be the first statement in a query batch.
A 'GO' in between the USE and CREATE is also not working.
Post #1389558
John Mitchell-245523
John Mitchell-245523
Posted Wednesday, November 28, 2012 1:37 AM
SSCarpal Tunnel
Group: General Forum Members
Last Login: Today @ 9:14 AM
Points: 4,428,
Visits: 7,196
I don't know if this would work for you, but you could create the schema in the model database. Then it would appear automatically in all new databases.
John
Post #1389565
WolfgangE
WolfgangE
Posted Wednesday, November 28, 2012 1:39 AM
Valued Member
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
this one works:
set @sql = 'exec '+ QUOTENAME(@dbname) + '..sp_executesql N''create schema MyNewSchema'''
exec (@sql)
Post #1389568
Robbert Hof
Robbert Hof
Posted Wednesday, November 28, 2012 1:44 AM
Ten Centuries
Group: General Forum Members
Last Login: Yesterday @ 4:29 AM
Points: 1,282,
Visits: 744
weberharter you are great!!, it WORKS.
Thanks.
Post #1389571
Koen Verbeeck
Koen Verbeeck
Posted Wednesday, November 28, 2012 1:50 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Today @ 6:51 AM
Points: 9,373,
Visits: 6,470
weberharter (11/28/2012)
this one works:
set @sql = 'exec '+ QUOTENAME(@dbname) + '..sp_executesql N''create schema MyNewSchema'''
exec (@sql)
Congrats for proving me wrong
How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
Member of
LinkedIn
. My blog at
LessThanDot
.
MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1389574
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.