Showing posts with label dynamic. Show all posts
Showing posts with label dynamic. Show all posts

Tuesday, March 20, 2012

Creating table using dynamic SQL

I have the following dataset using dynamic SQL which works when i execute it, when i try to create a table using this dataset i cant see any fields. Does anyone know why dynamic SQL doesnt work ?

Declare @.TopRange int
Declare @.BottomRange int
Declare @.SQL Varchar(1000)

IF @.Param_leadage = '91+'
SET @.TopRange = 91
ELSE
Set @.TopRange = RTRIM(LEFT(REPLACE(@.Param_leadage,'-',''),2))


IF @.Param_leadage = '91+'
SET @.BottomRange = 4000
ELSE
Set @.BottomRange = LTRIM(RIGHT(REPLACE(@.Param_leadage,'-',''),2))

SET @.SQL = 'SELECT dbo.tblCustomer.idStatus, dbo.tblCustomer.idCustomer, dbo.tblCustomer.DateSigned' +
' FROM dbo.tblCustomer' +
' WHERE DateDiff(day, dbo.tblCustomer.DateSigned, GetDate()) >= ' + convert(varchar,@.TopRange) + ' AND DateDiff(day,dbo.tblCustomer.DateSigned, GetDate()) <= ' + convert(varchar,@.BottomRange)

IF @.Param_status = 'Online Churn'
SET @.SQL = @.SQL + ' AND dbo.tblCustomer.idStatus = 4 or dbo.tblCustomer.idStatus = 5 or dbo.tblCustomer.idStatus = 11'
ELSE
SET @.SQL = @.SQL + ' AND dbo.tblCustomer.idStatus = ' + @.Param_idstatus


EXEC(@.SQL)

If the Refresh Fields toolbar button enabled on the Data tab is enabled, try clicking it to get the fields. Or, plug in only the SELECT statement and hit Refresh Fields to get the fields.Then, change it to the original SQL. Finally, you can create the dataset fields manually.

|||

Hi Teo,

This isnt working, when i try to select a data field from the table textbox properties value dropdown box, it says 'Dataset1 has no fields'

When I enter it in manually it gives an error which is obviously to do with the fields not being in the scope.

In the Data screen within VSS 2005 the sql executes fine, when i refresh it prompts for parameters off which i enter and the data is returned, it is not recognising any fields when i try to access any fields from any controls..

it has something to do with the exec(sql) command and dynamic data i would suspect

I also tried doing the SQL first off which the table fields are recognised but as soon as i add the other portions such as the single quotes etc it gives the same error

Can someone please help

thanks

|||

Try putting this code in a stored proc and changing the command type to be stored proc.

The other option is to not use dynamic SQL, instead use an expression based query and construct the SQL using expression syntax e.g.

= Iif(Parameters!x.value = "91+", "Select...", "select ...")

Again, in this case I follow the previous advice of just putting in the SQL query first, hitting refresh so RS auto-generates the Filed definitions and then swapping in the expression

|||

@. is interpreted as a query parameter in the Query Designer. You can make the whole darn thing expression-based as Adam suggested or move this query to a stored procedure.

|||

thanks guys, i put it in a stored proc and it is working now

Friday, February 17, 2012

Creating Dynamic Variables

Is there any way I can create variables dynamically in SQL server procedures?

e.g., I initially declare variable
@.col1='1'
@.col2='0'

and in a for loop with variable @.i,

I have @.str='@.col'+@.i
if @.i=1 then the @.str='@.col1'
and try to insert the value this variable holds into a table as

insert into table1 values(@.str)

Here I want to insert the value of @.col1(which is 1) and NOT '@.col1'

I tried so many time unsuccessfully.Has anyone done this kind of stuff before?any work arounds for this?

appreciate u'r help..

Thanks!Chances are you can accomplish your objectives using standard SQL, with maybe a little help from temporary tables, table variables, or case statements. I don't understand what you are trying to do, but your code looks like SQL written by a VB programmer (loops, inserting parameters, etc...). These are legitimate tools for SQL, but most procedures don't require them.

Using a dynamic SQL statement may be an option, either to assign the value to @.str or for the insert statement, but dynamic SQL is tricky because it executes in a distinct environment and your variables go out of scope.

If you could briefly describe your application, relevant table structures, and the task you are trying to perform, I may be able to give your some programming algorythms that are more appropriate for SQL.

blindman

creating dynamic translation

hi

is there a way to create a dynamic translation for a cube ?

i have another table that translate words and i want to that the cube dimensions names and the measures name will translate by the table i have

is there a way i can do it ?

Thanks

Eyal

Hi,

AS2005 can do what you described for attribute members, but not for cube dimension names and measure names. For attribute members, in the dimension editor (in BI Development Studio), in the Translations tab, you can specify for each attribute the column (from the relational table) from which to get the translated member names. For everything else (dimension names, hierarchy names, cube dimension names, measure names etc) AS2005 allows specifying a static text per language (the translated name).

However, you can use AMO (the management object model for AS2005) to dynamically read from the relational data table and create a Translation (with the static name) for each cube dimension and measure.

If AS2005 were to support natively this scenario, on each process of the cube, you would have gotten the new translated names for cube dimensions and measures. But if you do not have frequest changes in the translations table, then it should be acceptable to create the translations manually (and only update them from time to time, not necessarily on each cube re-process).

Quick startup on AMO: http://adriandu.spaces.live.com/

To create the translated names for cube dimensions and measures, there are the CubeDimension and Measure classes, each having a collection of Translation objects; each Translation specifies the Language (as an LCID) and the Caption (the text you will read from the relational translations table).

Adrian Dumitrascu

Creating dynamic table in SP

Hi all,
I have to create a dynamic table in a stored procedure, where it's name is
dynamic
I tried the following syntax:
select @.sql = 'create table #' + @.tbl_nm + ' (' + @.tbl_columns + ')'
execute (@.sql)
select @.sql = 'insert into ' + @.tbl_nm + ' select * from aaa_tbl '
execute(@.sql)
It said table not found when I tried to insert data to the newly created
table.
It seems the temporary table can be successfully created, but it is not
within the scope of the stored procedure and therefore, I cannot access the
table created using the above the syntax.
How could I solved this problem?
Thanks in advance.
--
Regards,
JoannaJoanna,
Are you really sure you need to use dynamic SQL here? If you're doing
'select * from aaa_tbl', then surely you know how to define your temporary
table, because it matches aaa_tbl? (I'm sure it's more complicated than
this, but I'd like to know why)
You're creating the temporary table within the context of the execute
statement, not within the context of the stored procedure itself.
Rob
"Joanna" <joanna@.abc.com> wrote in message
news:%23TDXEZRiGHA.1208@.TK2MSFTNGP02.phx.gbl...
> Hi all,
> I have to create a dynamic table in a stored procedure, where it's name is
> dynamic
> I tried the following syntax:
> select @.sql = 'create table #' + @.tbl_nm + ' (' + @.tbl_columns + ')'
> execute (@.sql)
> select @.sql = 'insert into ' + @.tbl_nm + ' select * from aaa_tbl '
> execute(@.sql)
> It said table not found when I tried to insert data to the newly created
> table.
> It seems the temporary table can be successfully created, but it is not
> within the scope of the stored procedure and therefore, I cannot access
> the table created using the above the syntax.
> How could I solved this problem?
> Thanks in advance.
> --
> Regards,
> Joanna
>|||Hi Rob,

> Are you really sure you need to use dynamic SQL here? If you're doing
> 'select * from aaa_tbl', then surely you know how to define your temporary
> table, because it matches aaa_tbl? (I'm sure it's more complicated than
> this, but I'd like to know why)
It is because I want to have the table structure of the temporary table
exactly the same as aaa_tbl.
I have a SP to get the table structure of aaa_tbl and therefore, when the
table structure of aaa_tbl changed, I don't have to modify any code.
Also, the table name of the newly created table is dynamic and therefore, I
have to use dynamic SQL.
--
Regards,
Joanna
"Rob Farley" <rob_farley@.hotmail.com> glsD:OK%23HbdRiGHA.4276@.TK2MSFTNGP03.phx.gb
l...
> Joanna,
> Are you really sure you need to use dynamic SQL here? If you're doing
> 'select * from aaa_tbl', then surely you know how to define your temporary
> table, because it matches aaa_tbl? (I'm sure it's more complicated than
> this, but I'd like to know why)
> You're creating the temporary table within the context of the execute
> statement, not within the context of the stored procedure itself.
> Rob
>
> "Joanna" <joanna@.abc.com> wrote in message
> news:%23TDXEZRiGHA.1208@.TK2MSFTNGP02.phx.gbl...
>|||Joanna
An option would be execute a SELECT INTO statement as the structure of the
new table created by the SELECT INTO is defined by the attributes of the
expressions in the select list.
eg.
DECLARE @.tbl_nm SYSNAME
SET @.tbl_nm = 'foo'
EXEC('SELECT * INTO ' + @.tbl_nm + ' FROM aaa_tbl')
All the usual caveats apply to dynamic SQL such as SQL Injectsion etc. as
you do not want a user to be able to pass the table name in as a parameter
and instead pass in something like SET @.tbl_nm = 'foo from sysobjects; drop
database northwind; --'
HTH
- Peter Ward
WARDY IT Solutions
"Joanna" wrote:

> Hi all,
> I have to create a dynamic table in a stored procedure, where it's name is
> dynamic
> I tried the following syntax:
> select @.sql = 'create table #' + @.tbl_nm + ' (' + @.tbl_columns + ')'
> execute (@.sql)
> select @.sql = 'insert into ' + @.tbl_nm + ' select * from aaa_tbl '
> execute(@.sql)
> It said table not found when I tried to insert data to the newly created
> table.
> It seems the temporary table can be successfully created, but it is not
> within the scope of the stored procedure and therefore, I cannot access th
e
> table created using the above the syntax.
> How could I solved this problem?
> Thanks in advance.
> --
> Regards,
> Joanna
>
>|||Ok, it does sound like you really do want dynamic SQL here.
How are you going to query your table, if everything about it is dynamic?
And if you only want to query certain known columns, then perhaps you should
only query certain known columns in the first place?
"Joanna" <joanna@.abc.com> wrote in message
news:ez1C36RiGHA.1600@.TK2MSFTNGP04.phx.gbl...
> Hi Rob,
>
> It is because I want to have the table structure of the temporary table
> exactly the same as aaa_tbl.
> I have a SP to get the table structure of aaa_tbl and therefore, when the
> table structure of aaa_tbl changed, I don't have to modify any code.
> Also, the table name of the newly created table is dynamic and therefore,
> I have to use dynamic SQL.
> --
> Regards,
> Joanna
> "Rob Farley" <rob_farley@.hotmail.com>
> glsD:OK%23HbdRiGHA.4276@.TK2MSFTNGP03.phx.gbl...
>|||Joanna,
How about
select * into #aaa_tbl
from aaa_tbl
where 1 = 0
-- Steve Kass
-- Drew University
-- http://www.stevekass.com
Joanna wrote:

>Hi Rob,
>
>
>It is because I want to have the table structure of the temporary table
>exactly the same as aaa_tbl.
>I have a SP to get the table structure of aaa_tbl and therefore, when the
>table structure of aaa_tbl changed, I don't have to modify any code.
>Also, the table name of the newly created table is dynamic and therefore, I
>have to use dynamic SQL.
>|||Probably won't help her Steve, as aaa_tbl might not be the name of the
table.
"Steve Kass" <skass@.drew.edu> wrote in message
news:%23RqQNmSiGHA.3440@.TK2MSFTNGP02.phx.gbl...
> Joanna,
> How about
> select * into #aaa_tbl
> from aaa_tbl
> where 1 = 0
> -- Steve Kass
> -- Drew University
> -- http://www.stevekass.com
> Joanna wrote:
>|||Joanna (joanna@.abc.com) writes:
> It is because I want to have the table structure of the temporary table
> exactly the same as aaa_tbl.
> I have a SP to get the table structure of aaa_tbl and therefore, when the
> table structure of aaa_tbl changed, I don't have to modify any code.
> Also, the table name of the newly created table is dynamic and
> therefore, I have to use dynamic SQL.
What are you real business requirements?
A database is supposed to have a stable schema. Table may be changed
with application upgrades, but when the system is live, tables should
not be created, dropped or altered.
While the solution for your problem as stated may be to use a global
temp table with two ##, it would mainly help to reinforce the impression
that you have painted yourself into the wrong corner. Creating tables
dynamically, reading its schema etc is not aimed to get the best performance
out of the system.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Creating dynamic reports

Hello.

I wanted to know a way to create dynamic reports using reporting / analysis services in SQL Server 2005 / SSIS tool.

I want user to give inputs and generate reports based on those inputs.

Please guide me on this issue.

Thank you.

Regards,

Prathamesh

Hello Domnick....

In Report Services, you have basically two ways to do this....

1- The Input Paremeter can stay in the Report Services

2- The Input Parameter can stay in application and can be sent to Report Services by the Url ( Named Url Access )

1--> If in Query of your DataSet, you have a parameter (Ex: @.ID), Automatically it will be detected by Report Services, and it can be edited by the Report Parematers -->In Layout View / Right Click at the Fund Plane of Your Report / Report Parameters

Or the input parameter can be added manually in this same location (I don′t recomended)

2--> Sample :

http://server/reportserver?/Sales/Northwest/Employee Sales Report&rs:Command=Render&ID=1234

Is this your problem?

Creating Dynamic MDX Queries within SQL Server 2005 Reporting Services

Please I need some help urgently, How can I create Dynamic MDX Queries
within SQL Server 2005 Reporting Services to use Parameters created
from SQL Queries Dataset.
I was able to do this in SQL Server 2000 Reporting Services by
replacing MDX Statement (1) with (2) below, but I notice the symbol ="
at the begining of statement (2) is not allowed.
(1)
SELECT NON EMPTY { [Measures].[Expected Gross Ult Premium],
[Measures].[Expected Ultimate Deductions Pct], [Measures].[IELR Pct],
[Measures].[Expected Composite Ratio Pct], [Measures].[Booked Premium],
[Measures].[Booked Deduction Pct], [Measures].[Booked Paid Loss Pct],
[Measures].[Booked Incurred Loss Pct], [Measures].[Booked Composite
Pct], [Measures].[Expected NPV], [Measures].[Expected NPV Pct],
[Measures].[Expected ROE], [Measures].[Allocated Capital],
[Measures].[Booked Tech Margin Inc Cat] } ON COLUMNS ,
NON EMPTY { DESCENDANTS( [Underwriting Year].[Underwriting Year].[All
Underwriting Years], [Underwriting Year].[Underwriting
Year].[Underwriting Year] ) } ON ROWS
FROM [GroupReporting]
WHERE ( [Transaction Original Currency].[Currency].&[85], [Lifetime
Contract].[Lifetime Contract].[Originating Contract
Reference].&[C12664] )
CELL PROPERTIES VALUE, FORMATTED_VALUE, CELL_ORDINAL, ACTION_TYPE
(2)
="SELECT NON EMPTY { [Measures].[Expected Gross Ult Premium],
[Measures].[Expected Ultimate Deductions Pct], [Measures].[IELR Pct],
[Measures].[Expected Composite Ratio Pct], [Measures].[Booked Premium],
[Measures].[Booked Deduction Pct], [Measures].[Booked Paid Loss Pct],
[Measures].[Booked Incurred Loss Pct], [Measures].[Booked Composite
Pct], [Measures].[Expected NPV], [Measures].[Expected NPV Pct],
[Measures].[Expected ROE], [Measures].[Allocated Capital],
[Measures].[Booked Tech Margin Inc Cat] } ON COLUMNS , NON EMPTY {
DESCENDANTS( [Underwriting Year].[Underwriting Year].[All Underwriting
Years], [Underwriting Year].[Underwriting Year].[Underwriting Year] ) }
ON ROWS
FROM [GroupReporting]
WHERE ( [Transaction Original Currency].[Currency].&[" &
Parameters!CurrencyCode.Value & "], [Lifetime Contract].[Lifetime
Contract].[Originating Contract Reference].&[" &
Parameters!MISOriginatingContractID.Value & "] ) CELL PROPERTIES
VALUE, FORMATTED_VALUE, CELL_ORDINAL, FORMAT_STRING"You need to change it to be an expression.
Open the dataset properties window.
Make sure the command type is text,
Click Expression Builder (fx) next to the Query String input box, and then
Enter your expression. You won't be able to execute it anymore in the
dataset window, but it will
execute when you refresh your dataset and go to use it.
<michael.oseni@.aleagroup.com> wrote in message
news:1157464471.474318.252920@.d34g2000cwd.googlegroups.com...
> Please I need some help urgently, How can I create Dynamic MDX Queries
> within SQL Server 2005 Reporting Services to use Parameters created
> from SQL Queries Dataset.
> I was able to do this in SQL Server 2000 Reporting Services by
> replacing MDX Statement (1) with (2) below, but I notice the symbol ="
> at the begining of statement (2) is not allowed.
> (1)
> SELECT NON EMPTY { [Measures].[Expected Gross Ult Premium],
> [Measures].[Expected Ultimate Deductions Pct], [Measures].[IELR Pct],
> [Measures].[Expected Composite Ratio Pct], [Measures].[Booked Premium],
> [Measures].[Booked Deduction Pct], [Measures].[Booked Paid Loss Pct],
> [Measures].[Booked Incurred Loss Pct], [Measures].[Booked Composite
> Pct], [Measures].[Expected NPV], [Measures].[Expected NPV Pct],
> [Measures].[Expected ROE], [Measures].[Allocated Capital],
> [Measures].[Booked Tech Margin Inc Cat] } ON COLUMNS ,
> NON EMPTY { DESCENDANTS( [Underwriting Year].[Underwriting Year].[All
> Underwriting Years], [Underwriting Year].[Underwriting
> Year].[Underwriting Year] ) } ON ROWS
> FROM [GroupReporting]
> WHERE ( [Transaction Original Currency].[Currency].&[85], [Lifetime
> Contract].[Lifetime Contract].[Originating Contract
> Reference].&[C12664] )
> CELL PROPERTIES VALUE, FORMATTED_VALUE, CELL_ORDINAL, ACTION_TYPE
> (2)
> ="SELECT NON EMPTY { [Measures].[Expected Gross Ult Premium],
> [Measures].[Expected Ultimate Deductions Pct], [Measures].[IELR Pct],
> [Measures].[Expected Composite Ratio Pct], [Measures].[Booked Premium],
> [Measures].[Booked Deduction Pct], [Measures].[Booked Paid Loss Pct],
> [Measures].[Booked Incurred Loss Pct], [Measures].[Booked Composite
> Pct], [Measures].[Expected NPV], [Measures].[Expected NPV Pct],
> [Measures].[Expected ROE], [Measures].[Allocated Capital],
> [Measures].[Booked Tech Margin Inc Cat] } ON COLUMNS , NON EMPTY {
> DESCENDANTS( [Underwriting Year].[Underwriting Year].[All Underwriting
> Years], [Underwriting Year].[Underwriting Year].[Underwriting Year] ) }
> ON ROWS
> FROM [GroupReporting]
> WHERE ( [Transaction Original Currency].[Currency].&[" &
> Parameters!CurrencyCode.Value & "], [Lifetime Contract].[Lifetime
> Contract].[Originating Contract Reference].&[" &
> Parameters!MISOriginatingContractID.Value & "] ) CELL PROPERTIES
> VALUE, FORMATTED_VALUE, CELL_ORDINAL, FORMAT_STRING"
>|||Thanks Chris, your below solution works fine.
Chris wrote:
> You need to change it to be an expression.
> Open the dataset properties window.
> Make sure the command type is text,
> Click Expression Builder (fx) next to the Query String input box, and then
> Enter your expression. You won't be able to execute it anymore in the
> dataset window, but it will
> execute when you refresh your dataset and go to use it.
>
> <michael.oseni@.aleagroup.com> wrote in message
> news:1157464471.474318.252920@.d34g2000cwd.googlegroups.com...
> > Please I need some help urgently, How can I create Dynamic MDX Queries
> > within SQL Server 2005 Reporting Services to use Parameters created
> > from SQL Queries Dataset.
> >
> > I was able to do this in SQL Server 2000 Reporting Services by
> > replacing MDX Statement (1) with (2) below, but I notice the symbol ="
> > at the begining of statement (2) is not allowed.
> >
> > (1)
> > SELECT NON EMPTY { [Measures].[Expected Gross Ult Premium],
> > [Measures].[Expected Ultimate Deductions Pct], [Measures].[IELR Pct],
> > [Measures].[Expected Composite Ratio Pct], [Measures].[Booked Premium],
> > [Measures].[Booked Deduction Pct], [Measures].[Booked Paid Loss Pct],
> > [Measures].[Booked Incurred Loss Pct], [Measures].[Booked Composite
> > Pct], [Measures].[Expected NPV], [Measures].[Expected NPV Pct],
> > [Measures].[Expected ROE], [Measures].[Allocated Capital],
> > [Measures].[Booked Tech Margin Inc Cat] } ON COLUMNS ,
> >
> > NON EMPTY { DESCENDANTS( [Underwriting Year].[Underwriting Year].[All
> > Underwriting Years], [Underwriting Year].[Underwriting
> > Year].[Underwriting Year] ) } ON ROWS
> >
> > FROM [GroupReporting]
> >
> > WHERE ( [Transaction Original Currency].[Currency].&[85], [Lifetime
> > Contract].[Lifetime Contract].[Originating Contract
> > Reference].&[C12664] )
> > CELL PROPERTIES VALUE, FORMATTED_VALUE, CELL_ORDINAL, ACTION_TYPE
> >
> > (2)
> > ="SELECT NON EMPTY { [Measures].[Expected Gross Ult Premium],
> > [Measures].[Expected Ultimate Deductions Pct], [Measures].[IELR Pct],
> > [Measures].[Expected Composite Ratio Pct], [Measures].[Booked Premium],
> > [Measures].[Booked Deduction Pct], [Measures].[Booked Paid Loss Pct],
> > [Measures].[Booked Incurred Loss Pct], [Measures].[Booked Composite
> > Pct], [Measures].[Expected NPV], [Measures].[Expected NPV Pct],
> > [Measures].[Expected ROE], [Measures].[Allocated Capital],
> > [Measures].[Booked Tech Margin Inc Cat] } ON COLUMNS , NON EMPTY {
> > DESCENDANTS( [Underwriting Year].[Underwriting Year].[All Underwriting
> > Years], [Underwriting Year].[Underwriting Year].[Underwriting Year] ) }
> > ON ROWS
> > FROM [GroupReporting]
> > WHERE ( [Transaction Original Currency].[Currency].&[" &
> > Parameters!CurrencyCode.Value & "], [Lifetime Contract].[Lifetime
> > Contract].[Originating Contract Reference].&[" &
> > Parameters!MISOriginatingContractID.Value & "] ) CELL PROPERTIES
> > VALUE, FORMATTED_VALUE, CELL_ORDINAL, FORMAT_STRING"
> >

Creating Dynamic dimension Security with Custom Procedure

Hi All,

We are trying to implement dynamic dimension security using a Custom procedure written in C#.

If we create a single entry in the return set everything works fine (See Below)

Microsoft.AnalysisServices.AdomdServer.Expression Expression = new Microsoft.AnalysisServices.AdomdServer.Expression();

Expression.ExpressionText = "[Sub Branch Id].[All Branch].[4]";

Microsoft.AnalysisServices.AdomdServer.Member Member = Expression.CalculateMdxObject(null).ToMember();

TupleBuilder.Add(Member);

SetBuilder.Add(TupleBuilder.ToTuple());

However when we try and add a second item to the Tuple builder the object returns the error : The 'Sub Branch Id' Hierarchy appears more than once in the tuple.

Code snippet :

{

Microsoft.AnalysisServices.AdomdServer.Expression Expression = new Microsoft.AnalysisServices.AdomdServer.Expression();

Expression.ExpressionText = "[Sub Branch Id].[All Branch].[2]";

Microsoft.AnalysisServices.AdomdServer.Member Member = Expression.CalculateMdxObject(null).ToMember();

TupleBuilder.Add(Member);

Expression=null;

Member=null;

}

{

Microsoft.AnalysisServices.AdomdServer.Expression Expression = new Microsoft.AnalysisServices.AdomdServer.Expression();

Expression.ExpressionText = "[Sub Branch Id].[All Branch].[3]";

Microsoft.AnalysisServices.AdomdServer.Member Member = Expression.CalculateMdxObject(null).ToMember();

TupleBuilder.Add(Member);

Expression=null;

Member=null;

}

SetBuilder.Add(TupleBuilder.ToTuple());

Please can some one help resolve this issue as its becoming critical for the project that we are currently working on, otherwise we will have to revisit the security model.

TIA

Hello,

most likely you don't create a new tuple for each new member. Create a new instance of TupleBuilder to resolve it.

Radim

|||

Thanks for the Help Radmin, It was that.

The source example we're using was from Teo Lacevs book, applied MS Analysis Services 2005. On Teo's Forum we have found the correct syntax (though not in C#), and it does work.

This is the link to the solution, http://prologika.com/CS/forums/thread/1861.aspx

Creating Dynamic Database using Script

I am looking for a way to dynamically create a database in SQL Server
2000 using a stored procedure that will create a dynamically named new
database using the script from another database.
More specifically, I would like to call a stored procedure that will
pass in a new DB name and will take the script that I have created from
another database and simply create a new database with the new name
using the old scripts table structure, etc. It might take reading in
the script and passing in a dynamic variable, but I am having trouble
with it, as everytime I try to create a stored procedure with the
database script in it and substitute the variable @.NEWDBNAME in the
place of each instance of the database name in the script, I get errors
when trying to save the SP.
Any help with this would be hugely appreciated.Hi
Posting your code would have been useful!
You will need to make the whole statement "dynamic" and then execute the
statement.
CREATE PROCEDURE spr_create_new_database
AS
BEGIN
DECLARE @.dbname sysname
DECLARE @.stmt varchar(4000)
/* Create New ISD Backup database dynamically */
SET @.dbname = 'BackupDB_' +
REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'
','')
SET @.stmt = 'CREATE DATABASE ' + @.dbname
EXEC ( @.stmt )
RETURN
END
If you are going to create this database regularly then you may want to
create a template database and copy the file and use sp_attach_db to create a
nwe database, alternatively you could use backup/restore. If you want to
create a copy of your live database using RESTORE may be a quicker option
that creating the tables and pulling the data across.
You could use a DTS package to populate the database with a dynamic
properties task and a Copy SQL Server Objects task. This would allow you to
set the destination database without having to change the package.
John
"SQL Server Group 1" wrote:
> I am looking for a way to dynamically create a database in SQL Server
> 2000 using a stored procedure that will create a dynamically named new
> database using the script from another database.
> More specifically, I would like to call a stored procedure that will
> pass in a new DB name and will take the script that I have created from
> another database and simply create a new database with the new name
> using the old scripts table structure, etc. It might take reading in
> the script and passing in a dynamic variable, but I am having trouble
> with it, as everytime I try to create a stored procedure with the
> database script in it and substitute the variable @.NEWDBNAME in the
> place of each instance of the database name in the script, I get errors
> when trying to save the SP.
> Any help with this would be hugely appreciated.
>|||Thanks John for the reply. I am now in the middle of this and need
just a little clarification.
What I did to this point is alter the script to create a database named
"Connect_Template". Then I created the database from the query
analyzer from within the master database and saved the DTS package that
did that. Now I have a database called "Connect_Templage" and a DTS
package called "Create New DB From Termplate".
What I need to do from here is to create a SP that will create a new
database from that DTS package with a new name being passed in. Is
there a way to do that?
In my case, every time a new customer gets created, a new database
based on that template structure needs to be created and I can then
dynamically add a DSN to it from my end. I just need to figure out
what methodis best and will work.
The script to create my database is really really long, but I wonder if
the sp_detach_db and sp_attach_db would work passing in dynamic names?
Thanks again for your help...you have given me a lot of options. I just
need one that would easily work.
I am trying your dynamic statements now but am wondering if the DTP
method would work passing in a dynamic variable.
John Bell wrote:
> Hi
> Posting your code would have been useful!
> You will need to make the whole statement "dynamic" and then execute the
> statement.
> CREATE PROCEDURE spr_create_new_database
> AS
> BEGIN
> DECLARE @.dbname sysname
> DECLARE @.stmt varchar(4000)
> /* Create New ISD Backup database dynamically */
> SET @.dbname = 'BackupDB_' +
> REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'
> ','')
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
> If you are going to create this database regularly then you may want to
> create a template database and copy the file and use sp_attach_db to create a
> nwe database, alternatively you could use backup/restore. If you want to
> create a copy of your live database using RESTORE may be a quicker option
> that creating the tables and pulling the data across.
> You could use a DTS package to populate the database with a dynamic
> properties task and a Copy SQL Server Objects task. This would allow you to
> set the destination database without having to change the package.
> John
> "SQL Server Group 1" wrote:
> > I am looking for a way to dynamically create a database in SQL Server
> > 2000 using a stored procedure that will create a dynamically named new
> > database using the script from another database.
> >
> > More specifically, I would like to call a stored procedure that will
> > pass in a new DB name and will take the script that I have created from
> > another database and simply create a new database with the new name
> > using the old scripts table structure, etc. It might take reading in
> > the script and passing in a dynamic variable, but I am having trouble
> > with it, as everytime I try to create a stored procedure with the
> > database script in it and substitute the variable @.NEWDBNAME in the
> > place of each instance of the database name in the script, I get errors
> > when trying to save the SP.
> >
> > Any help with this would be hugely appreciated.
> >
> >|||Just for a try, I ran the code below from the Master Database, and it
compiled and worked.
I think that might be the best way to run that. The only thing is now,
how would be the best way to run the create table structures, SPs, etc.
once the new database gets created? How would I create the DTS package
with a dynamic properties task? I have used DTS to import and export
from specific places but not anything dynamically.
Thanks again...this is really helping!
John Bell wrote:
> Hi
> Posting your code would have been useful!
> You will need to make the whole statement "dynamic" and then execute the
> statement.
> CREATE PROCEDURE spr_create_new_database
> AS
> BEGIN
> DECLARE @.dbname sysname
> DECLARE @.stmt varchar(4000)
> /* Create New ISD Backup database dynamically */
> SET @.dbname = 'BackupDB_' +
> REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'
> ','')
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
> If you are going to create this database regularly then you may want to
> create a template database and copy the file and use sp_attach_db to create a
> nwe database, alternatively you could use backup/restore. If you want to
> create a copy of your live database using RESTORE may be a quicker option
> that creating the tables and pulling the data across.
> You could use a DTS package to populate the database with a dynamic
> properties task and a Copy SQL Server Objects task. This would allow you to
> set the destination database without having to change the package.
> John
> "SQL Server Group 1" wrote:
> > I am looking for a way to dynamically create a database in SQL Server
> > 2000 using a stored procedure that will create a dynamically named new
> > database using the script from another database.
> >
> > More specifically, I would like to call a stored procedure that will
> > pass in a new DB name and will take the script that I have created from
> > another database and simply create a new database with the new name
> > using the old scripts table structure, etc. It might take reading in
> > the script and passing in a dynamic variable, but I am having trouble
> > with it, as everytime I try to create a stored procedure with the
> > database script in it and substitute the variable @.NEWDBNAME in the
> > place of each instance of the database name in the script, I get errors
> > when trying to save the SP.
> >
> > Any help with this would be hugely appreciated.
> >
> >|||Hi
The procedure is fine if you want to create a database with a given name,
but to use that else where you should really create the name and pass it as a
parameter to the stored procedure. You can then call a DTS package and pass
the database name as a global variable using xp_cmdshell to call DTSRUN see
http://www.sqldts.com/default.aspx?301 about getting help on the command
syntax.
Using this global variable in the DTS package, it can be assigned using the
dynamic properties task can assign it to the destination of a Copy SQL Server
objects task.
e.g.
CREATE PROCEDURE spr_create_new_database ( @.dbname sysname )
AS
BEGIN
DECLARE @.stmt varchar(4000)
/* Create New Backup database dynamically */
SET @.stmt = 'CREATE DATABASE ' + @.dbname
EXEC ( @.stmt )
RETURN
END
Within the job step you can then do something like:
DECLARE @.cmd varchar(4000)
DECLARE @.dbname sysname
SET @.dbname = 'BackupDB_' +
REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'','')
EXEC spr_create_new_database @.dbname
SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N "DTS Package" '
SET @.cmd = @.cmd + ' /A " Backup Database Name":"8"="' + @.dbname + '"'
EXEC master..xp_cmdshell @.cmd
This will run the dts package called "DTS Package" on the current server
using trusted authentication, and pass the global variable @.dbname which has
just been used to create the backup database. The global variable is called
"Backup Database Name" You could even pass the source database name as a
global variable and also assign that in the dynamic properties task to make
it totally flexible.
Before you create the DTS package run the stored procedure first so that a
destination database exists for the Copy SQL Objects task. When you create
the DTS Package, create Copy SQL Objects task first and test it, before
creating the global variables and then finally create the Dynamic Properties
task and the workflow to make sure that the Dynamic Properties task runs
before the Copy SQL Objects task.
HTH
John
"SQL Server Group 1" wrote:
> Just for a try, I ran the code below from the Master Database, and it
> compiled and worked.
> I think that might be the best way to run that. The only thing is now,
> how would be the best way to run the create table structures, SPs, etc.
> once the new database gets created? How would I create the DTS package
> with a dynamic properties task? I have used DTS to import and export
> from specific places but not anything dynamically.
> Thanks again...this is really helping!
>
> John Bell wrote:
> > Hi
> >
> > Posting your code would have been useful!
> >
> > You will need to make the whole statement "dynamic" and then execute the
> > statement.
> >
> > CREATE PROCEDURE spr_create_new_database
> > AS
> > BEGIN
> > DECLARE @.dbname sysname
> > DECLARE @.stmt varchar(4000)
> >
> > /* Create New ISD Backup database dynamically */
> > SET @.dbname = 'BackupDB_' +
> > REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'
> > ','')
> >
> > SET @.stmt = 'CREATE DATABASE ' + @.dbname
> >
> > EXEC ( @.stmt )
> >
> > RETURN
> > END
> >
> > If you are going to create this database regularly then you may want to
> > create a template database and copy the file and use sp_attach_db to create a
> > nwe database, alternatively you could use backup/restore. If you want to
> > create a copy of your live database using RESTORE may be a quicker option
> > that creating the tables and pulling the data across.
> >
> > You could use a DTS package to populate the database with a dynamic
> > properties task and a Copy SQL Server Objects task. This would allow you to
> > set the destination database without having to change the package.
> >
> > John
> >
> > "SQL Server Group 1" wrote:
> >
> > > I am looking for a way to dynamically create a database in SQL Server
> > > 2000 using a stored procedure that will create a dynamically named new
> > > database using the script from another database.
> > >
> > > More specifically, I would like to call a stored procedure that will
> > > pass in a new DB name and will take the script that I have created from
> > > another database and simply create a new database with the new name
> > > using the old scripts table structure, etc. It might take reading in
> > > the script and passing in a dynamic variable, but I am having trouble
> > > with it, as everytime I try to create a stored procedure with the
> > > database script in it and substitute the variable @.NEWDBNAME in the
> > > place of each instance of the database name in the script, I get errors
> > > when trying to save the SP.
> > >
> > > Any help with this would be hugely appreciated.
> > >
> > >
>|||I really appreciate the help...I am sort of new to the advanced DTS
stuff but have used DTS for import, export. So I will give this a try
and see if I can get it.
Thanks for the link. In creating the Advanced DTS run object, what
would be the dynamic variable name for the database name that is passed
to the copy object? Would it be in the Properties List in the Dynamic
Properties task component under Tasks > Copy SQL Server Objects? For
example: DestinationDatabase, DestinationLogin, DestinationPassword?
If that's right, I think I can do this...
John Bell wrote:
> Hi
> The procedure is fine if you want to create a database with a given name,
> but to use that else where you should really create the name and pass it as a
> parameter to the stored procedure. You can then call a DTS package and pass
> the database name as a global variable using xp_cmdshell to call DTSRUN see
> http://www.sqldts.com/default.aspx?301 about getting help on the command
> syntax.
> Using this global variable in the DTS package, it can be assigned using the
> dynamic properties task can assign it to the destination of a Copy SQL Server
> objects task.
> e.g.
> CREATE PROCEDURE spr_create_new_database ( @.dbname sysname )
> AS
> BEGIN
> DECLARE @.stmt varchar(4000)
> /* Create New Backup database dynamically */
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
> Within the job step you can then do something like:
> DECLARE @.cmd varchar(4000)
> DECLARE @.dbname sysname
> SET @.dbname = 'BackupDB_' +
> REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'','')
> EXEC spr_create_new_database @.dbname
> SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N "DTS Package" '
> SET @.cmd = @.cmd + ' /A " Backup Database Name":"8"="' + @.dbname + '"'
> EXEC master..xp_cmdshell @.cmd
> This will run the dts package called "DTS Package" on the current server
> using trusted authentication, and pass the global variable @.dbname which has
> just been used to create the backup database. The global variable is called
> "Backup Database Name" You could even pass the source database name as a
> global variable and also assign that in the dynamic properties task to make
> it totally flexible.
> Before you create the DTS package run the stored procedure first so that a
> destination database exists for the Copy SQL Objects task. When you create
> the DTS Package, create Copy SQL Objects task first and test it, before
> creating the global variables and then finally create the Dynamic Properties
> task and the workflow to make sure that the Dynamic Properties task runs
> before the Copy SQL Objects task.
> HTH
> John
> "SQL Server Group 1" wrote:
> > Just for a try, I ran the code below from the Master Database, and it
> > compiled and worked.
> >
> > I think that might be the best way to run that. The only thing is now,
> > how would be the best way to run the create table structures, SPs, etc.
> > once the new database gets created? How would I create the DTS package
> > with a dynamic properties task? I have used DTS to import and export
> > from specific places but not anything dynamically.
> >
> > Thanks again...this is really helping!
> >
> >
> >
> > John Bell wrote:
> > > Hi
> > >
> > > Posting your code would have been useful!
> > >
> > > You will need to make the whole statement "dynamic" and then execute the
> > > statement.
> > >
> > > CREATE PROCEDURE spr_create_new_database
> > > AS
> > > BEGIN
> > > DECLARE @.dbname sysname
> > > DECLARE @.stmt varchar(4000)
> > >
> > > /* Create New ISD Backup database dynamically */
> > > SET @.dbname = 'BackupDB_' +
> > > REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'
> > > ','')
> > >
> > > SET @.stmt = 'CREATE DATABASE ' + @.dbname
> > >
> > > EXEC ( @.stmt )
> > >
> > > RETURN
> > > END
> > >
> > > If you are going to create this database regularly then you may want to
> > > create a template database and copy the file and use sp_attach_db to create a
> > > nwe database, alternatively you could use backup/restore. If you want to
> > > create a copy of your live database using RESTORE may be a quicker option
> > > that creating the tables and pulling the data across.
> > >
> > > You could use a DTS package to populate the database with a dynamic
> > > properties task and a Copy SQL Server Objects task. This would allow you to
> > > set the destination database without having to change the package.
> > >
> > > John
> > >
> > > "SQL Server Group 1" wrote:
> > >
> > > > I am looking for a way to dynamically create a database in SQL Server
> > > > 2000 using a stored procedure that will create a dynamically named new
> > > > database using the script from another database.
> > > >
> > > > More specifically, I would like to call a stored procedure that will
> > > > pass in a new DB name and will take the script that I have created from
> > > > another database and simply create a new database with the new name
> > > > using the old scripts table structure, etc. It might take reading in
> > > > the script and passing in a dynamic variable, but I am having trouble
> > > > with it, as everytime I try to create a stored procedure with the
> > > > database script in it and substitute the variable @.NEWDBNAME in the
> > > > place of each instance of the database name in the script, I get errors
> > > > when trying to save the SP.
> > > >
> > > > Any help with this would be hugely appreciated.
> > > >
> > > >
> >
> >|||Hi
If you only have the two tasks then the will be called something like
DTSTask_DTSTransferObjectsTask_1
and DTSTask_DTSDynamicPropertiesTask_1
These will be listed under Tasks in the left hand pane of the "Dynamic
Properties Task: Package Properties" dialogue. Choose
DTSTask_DTSTransferObjectsTask_1.
On the right hand side a table of Property Names and Default Values will
appear, double click on DestinationDatabase in the Property Name column and
you will get the Add/Edit Assignment dialogue. Choose Global Variables as the
source and your Global variable will be in the name of the drop down.
HTH
John
"SQL Server Group 1" wrote:
> I really appreciate the help...I am sort of new to the advanced DTS
> stuff but have used DTS for import, export. So I will give this a try
> and see if I can get it.
> Thanks for the link. In creating the Advanced DTS run object, what
> would be the dynamic variable name for the database name that is passed
> to the copy object? Would it be in the Properties List in the Dynamic
> Properties task component under Tasks > Copy SQL Server Objects? For
> example: DestinationDatabase, DestinationLogin, DestinationPassword?
> If that's right, I think I can do this...
>
> John Bell wrote:
> > Hi
> >
> > The procedure is fine if you want to create a database with a given name,
> > but to use that else where you should really create the name and pass it as a
> > parameter to the stored procedure. You can then call a DTS package and pass
> > the database name as a global variable using xp_cmdshell to call DTSRUN see
> > http://www.sqldts.com/default.aspx?301 about getting help on the command
> > syntax.
> > Using this global variable in the DTS package, it can be assigned using the
> > dynamic properties task can assign it to the destination of a Copy SQL Server
> > objects task.
> >
> > e.g.
> >
> > CREATE PROCEDURE spr_create_new_database ( @.dbname sysname )
> > AS
> > BEGIN
> > DECLARE @.stmt varchar(4000)
> >
> > /* Create New Backup database dynamically */
> > SET @.stmt = 'CREATE DATABASE ' + @.dbname
> > EXEC ( @.stmt )
> > RETURN
> > END
> >
> > Within the job step you can then do something like:
> >
> > DECLARE @.cmd varchar(4000)
> > DECLARE @.dbname sysname
> >
> > SET @.dbname = 'BackupDB_' +
> > REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'','')
> >
> > EXEC spr_create_new_database @.dbname
> >
> > SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N "DTS Package" '
> > SET @.cmd = @.cmd + ' /A " Backup Database Name":"8"="' + @.dbname + '"'
> >
> > EXEC master..xp_cmdshell @.cmd
> >
> > This will run the dts package called "DTS Package" on the current server
> > using trusted authentication, and pass the global variable @.dbname which has
> > just been used to create the backup database. The global variable is called
> > "Backup Database Name" You could even pass the source database name as a
> > global variable and also assign that in the dynamic properties task to make
> > it totally flexible.
> >
> > Before you create the DTS package run the stored procedure first so that a
> > destination database exists for the Copy SQL Objects task. When you create
> > the DTS Package, create Copy SQL Objects task first and test it, before
> > creating the global variables and then finally create the Dynamic Properties
> > task and the workflow to make sure that the Dynamic Properties task runs
> > before the Copy SQL Objects task.
> >
> > HTH
> >
> > John
> > "SQL Server Group 1" wrote:
> >
> > > Just for a try, I ran the code below from the Master Database, and it
> > > compiled and worked.
> > >
> > > I think that might be the best way to run that. The only thing is now,
> > > how would be the best way to run the create table structures, SPs, etc.
> > > once the new database gets created? How would I create the DTS package
> > > with a dynamic properties task? I have used DTS to import and export
> > > from specific places but not anything dynamically.
> > >
> > > Thanks again...this is really helping!
> > >
> > >
> > >
> > > John Bell wrote:
> > > > Hi
> > > >
> > > > Posting your code would have been useful!
> > > >
> > > > You will need to make the whole statement "dynamic" and then execute the
> > > > statement.
> > > >
> > > > CREATE PROCEDURE spr_create_new_database
> > > > AS
> > > > BEGIN
> > > > DECLARE @.dbname sysname
> > > > DECLARE @.stmt varchar(4000)
> > > >
> > > > /* Create New ISD Backup database dynamically */
> > > > SET @.dbname = 'BackupDB_' +
> > > > REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),GETDATE(),121),':',''),'-',''),'
> > > > ','')
> > > >
> > > > SET @.stmt = 'CREATE DATABASE ' + @.dbname
> > > >
> > > > EXEC ( @.stmt )
> > > >
> > > > RETURN
> > > > END
> > > >
> > > > If you are going to create this database regularly then you may want to
> > > > create a template database and copy the file and use sp_attach_db to create a
> > > > nwe database, alternatively you could use backup/restore. If you want to
> > > > create a copy of your live database using RESTORE may be a quicker option
> > > > that creating the tables and pulling the data across.
> > > >
> > > > You could use a DTS package to populate the database with a dynamic
> > > > properties task and a Copy SQL Server Objects task. This would allow you to
> > > > set the destination database without having to change the package.
> > > >
> > > > John
> > > >
> > > > "SQL Server Group 1" wrote:
> > > >
> > > > > I am looking for a way to dynamically create a database in SQL Server
> > > > > 2000 using a stored procedure that will create a dynamically named new
> > > > > database using the script from another database.
> > > > >
> > > > > More specifically, I would like to call a stored procedure that will
> > > > > pass in a new DB name and will take the script that I have created from
> > > > > another database and simply create a new database with the new name
> > > > > using the old scripts table structure, etc. It might take reading in
> > > > > the script and passing in a dynamic variable, but I am having trouble
> > > > > with it, as everytime I try to create a stored procedure with the
> > > > > database script in it and substitute the variable @.NEWDBNAME in the
> > > > > place of each instance of the database name in the script, I get errors
> > > > > when trying to save the SP.
> > > > >
> > > > > Any help with this would be hugely appreciated.
> > > > >
> > > > >
> > >
> > >
>|||John,
I almost have it, I think. Here is the stored procedure code I am
running. It created the new database just fine, but it doesn't copy
the data from the template database.
CREATE PROCEDURE sprk_create_new_database ( @.dbname sysname )
AS
BEGIN
DECLARE @.stmt varchar(4000)
/* Create New Backup database dynamically */
SET @.stmt = 'CREATE DATABASE ' + @.dbname
EXEC ( @.stmt )
RETURN
END
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
"Create_New_Connect_DB" '
SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
EXEC master..xp_cmdshell @.cmd
GO
----.
The exec code is:
exec sprk_create_new_database @.dbname = 'TestCopy'
The resulting text is:
The CREATE DATABASE process is allocating 0.63 MB on disk 'TestCopy'.
The CREATE DATABASE process is allocating 0.49 MB on disk
'TestCopy_log'.
However, when it gets to the copy DTS package, "Create_New_Connect_DB",
it does not pass in the variable 'dbname' to the package that has a
Dynamic Properties Variable that passes that name into the
'DestinationDatabase' variable of the Copy SQL Server Objects Task.
Can you see anything here that looks wrong? Again, I really appreciate
the help. I think I'm really close to solving this one!
Brandon|||Hi
You are passing the name to the stored procedure rather than setting the
local variable with the name. The code will not work at all without the local
variable being declared!. This may be easier if you create a procedure to
both call your database creation procedure and the DTS Package (This may wrap)
CREATE PROCEDURE sprk_create_new_database_and_populate_it ( @.dbname sysname )
AS
BEGIN
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
"Create_New_Connect_DB" '
SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
EXEC master..xp_cmdshell @.cmd
END
GO
Then run sprk_create_new_database_and_populate_it passing the name of the
database you wish to populate:
EXEC sprk_create_new_database_and_populate_it @.dbname = 'TestCopy'
HTH
John
"SQL Server Group 1" wrote:
> John,
> I almost have it, I think. Here is the stored procedure code I am
> running. It created the new database just fine, but it doesn't copy
> the data from the template database.
> CREATE PROCEDURE sprk_create_new_database ( @.dbname sysname )
> AS
> BEGIN
> DECLARE @.stmt varchar(4000)
>
> /* Create New Backup database dynamically */
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
>
> DECLARE @.cmd varchar(4000)
> EXEC sprk_create_new_database @.dbname
>
> SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
> "Create_New_Connect_DB" '
> SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
>
> EXEC master..xp_cmdshell @.cmd
> GO
> ----.
> The exec code is:
> exec sprk_create_new_database @.dbname = 'TestCopy'
> The resulting text is:
> The CREATE DATABASE process is allocating 0.63 MB on disk 'TestCopy'.
> The CREATE DATABASE process is allocating 0.49 MB on disk
> 'TestCopy_log'.
> However, when it gets to the copy DTS package, "Create_New_Connect_DB",
> it does not pass in the variable 'dbname' to the package that has a
> Dynamic Properties Variable that passes that name into the
> 'DestinationDatabase' variable of the Copy SQL Server Objects Task.
> Can you see anything here that looks wrong? Again, I really appreciate
> the help. I think I'm really close to solving this one!
> Brandon
>|||OK, here are the two SPs
1) sprk_create_new_database_and_populate_it
CREATE PROCEDURE sprk_create_new_database_and_populate_it ( @.dbname
sysname )
AS
BEGIN
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
"Create_New_Connect_DB" '
SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
EXEC master..xp_cmdshell @.cmd
END
GO
2) sprk_create_new_database
CREATE PROCEDURE sprk_create_new_database ( @.dbname sysname )
AS
BEGIN
DECLARE @.stmt varchar(4000)
/* Create New Backup database dynamically */
SET @.stmt = 'CREATE DATABASE ' + @.dbname
EXEC ( @.stmt )
RETURN
END
GO
When I run:
EXEC sprk_create_new_database_and_populate_it @.dbname = 'TestCopy'
I get the message:
DTSRun: Must specify a value for switch: /N The parameter is
incorrect.
DTSRun: Invalid command options
Usage: dtsrun /option [value] [/option [value]] ...
Options ('/?' shows this screen; '-' May be substituted for '/'):
Package retrieval:
/~S Server Name
/~U User Name
/~P Password
/E <Use trusted connection instead of /U /P>
/~N Package Name
/~M Package Password
/~G Package Guid String
/~V Package Version Guid String
/~F Structured Storage UNC filename (overwritten if /S also
specified)
/~R Repository Database Name <uses default if blank; loads package
from repository database>
Package operation (overrides stored Package settings):
/~A Global Variable Name:typeid=Value <may quote entire string
(including name:typeid)>
/~L Log file name
/~W Write Completion Status to Windows Event Log <True or False>
DTSRun action (default is to execute Package):
/!X <Do not execute; retrieves Package to /F filename>
/!D <Do not execute; drop package from SQL Server (cannot drop from
Storage File)>
/!Y <Do not execute; output encrypted command line>
/!C <Copies command line to Windows clipboard (may be used with /!Y
and /!X)>
Notes:
~ is optional; if present, the parameter is hex text of encrypted
value (0x313233...)
Whitespace between command switch and value is optional
Embedded whitespace in values must be embedded in double-quotes
If an option is specified multiple times, the last one wins (except
multiple /A)
Error: -2147024809 (80070057); Provider Error: 0 (0)
Error string: DTSRun: Must specify a value for switch: /N The
parameter is incorrect.
Error source: Microsoft Data Transformation Services (DTS) Package
Help file: sqldts80.hlp
Help context: 0
NULL
Any thoughts...I does created the Database first, which is hopeful.
It just doesn't get through the population part.
Thanks!!|||Hi
Instead of "EXEC master..xp_cmdshell @.cmd" use SELECT @.cmd to print it out.
You could then see the whole command, and if necessary run it from a command
prompt. Check Create_New_Connect_DB is the package name (check no trailing
spaces) and you have saved it to the server.
You may want to compare what you are running against the output from
http://www.sqldts.com/default.aspx?301
John
"SQL Server Group 1" wrote:
> OK, here are the two SPs
> 1) sprk_create_new_database_and_populate_it
>
> CREATE PROCEDURE sprk_create_new_database_and_populate_it ( @.dbname
> sysname )
> AS
> BEGIN
> DECLARE @.cmd varchar(4000)
>
> EXEC sprk_create_new_database @.dbname
>
> SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
> "Create_New_Connect_DB" '
> SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
> EXEC master..xp_cmdshell @.cmd
> END
> GO
> 2) sprk_create_new_database
> CREATE PROCEDURE sprk_create_new_database ( @.dbname sysname )
> AS
> BEGIN
> DECLARE @.stmt varchar(4000)
>
> /* Create New Backup database dynamically */
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
> GO
>
> When I run:
> EXEC sprk_create_new_database_and_populate_it @.dbname = 'TestCopy'
> I get the message:
> DTSRun: Must specify a value for switch: /N The parameter is
> incorrect.
> DTSRun: Invalid command options
> Usage: dtsrun /option [value] [/option [value]] ...
> Options ('/?' shows this screen; '-' May be substituted for '/'):
> Package retrieval:
> /~S Server Name
> /~U User Name
> /~P Password
> /E <Use trusted connection instead of /U /P>
> /~N Package Name
> /~M Package Password
> /~G Package Guid String
> /~V Package Version Guid String
> /~F Structured Storage UNC filename (overwritten if /S also
> specified)
> /~R Repository Database Name <uses default if blank; loads package
> from repository database>
> Package operation (overrides stored Package settings):
> /~A Global Variable Name:typeid=Value <may quote entire string
> (including name:typeid)>
> /~L Log file name
> /~W Write Completion Status to Windows Event Log <True or False>
> DTSRun action (default is to execute Package):
> /!X <Do not execute; retrieves Package to /F filename>
> /!D <Do not execute; drop package from SQL Server (cannot drop from
> Storage File)>
> /!Y <Do not execute; output encrypted command line>
> /!C <Copies command line to Windows clipboard (may be used with /!Y
> and /!X)>
> Notes:
> ~ is optional; if present, the parameter is hex text of encrypted
> value (0x313233...)
> Whitespace between command switch and value is optional
> Embedded whitespace in values must be embedded in double-quotes
> If an option is specified multiple times, the last one wins (except
> multiple /A)
>
> Error: -2147024809 (80070057); Provider Error: 0 (0)
> Error string: DTSRun: Must specify a value for switch: /N The
> parameter is incorrect.
> Error source: Microsoft Data Transformation Services (DTS) Package
> Help file: sqldts80.hlp
> Help context: 0
> NULL
> Any thoughts...I does created the Database first, which is hopeful.
> It just doesn't get through the population part.
> Thanks!!
>|||OK..Last try.
I now have everything working if I pass in a preset variable, but I
can't seem to get the formatting right when I try to pass in the the
dynamic variable name "@.dbname"
Here is my query analyzer statement:
EXEC sprk_create_new_database_and_populate_it @.dbname = 'NewConnectDB'
Here is what works:
CREATE PROCEDURE sprk_create_new_database_and_populate_it ( @.dbname
sysname )
AS
BEGIN
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET
@.cmd = 'DTSRun /S "(local)" /N "3Click_Connect_Copy_DB" /G
"{AB3BD356-2FE4-49FB-8233-51C4F82C0B25}" /A
"DestDatabase":"8"="NewConnectDB" /W "0" /E'
EXEC master..xp_cmdshell @.cmd
END
Notice above that I made the DestDatabase a constant and not passing in
@.dbname. That works using the exec statement at the top.
Here is what does not work:
CREATE PROCEDURE sprk_create_new_database_and_populate_it ( @.dbname
sysname )
AS
BEGIN
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET
@.cmd = 'DTSRun /S "(local)" /N "3Click_Connect_Copy_DB" /G
"{AB3BD356-2FE4-49FB-8233-51C4F82C0B25}" /A "DestDatabase":"8"="'+@.dbname+'" /W "0" /E'
EXEC master..xp_cmdshell @.cmd
END
GO
This gives me an error:
Error string: [SQL-DMO]The name ' "NewConnectDB"' was not found in
the Databases collection. If the name is a qualified name, use [] to
separate various parts of the name, and try again.
I can't seem to format the @.dbname in the cmd statement that will pass
in the value NewConnectDB that is formatted properly. Can you see what
the problem might be?
Thanks again, as I have been working on this for quite a while and
think I am almost done.
Brandon
GO|||AWESOME!!!!
I figured it out finally.
Here is the DTS run string that works just for future reference:
CREATE PROCEDURE sprk_create_new_database_and_populate_it ( @.dbname
sysname )
AS
BEGIN
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET
@.cmd = 'DTSRun /S "(local)" /N "3Click_Connect_Copy_DB" /G
"{AB3BD356-2FE4-49FB-8233-51C4F82C0B25}" /A "DestDatabase":"8"=' +
@.dbname + ' /W "0" /E'
EXEC master..xp_cmdshell @.cmd
END
GO
I really, really apprciate your help and thank you for your patience.
I should have a good foundation on which to base my future dynamic DTS
packages!
Brandon

Creating Dynamic Database using Script

I am looking for a way to dynamically create a database in SQL Server
2000 using a stored procedure that will create a dynamically named new
database using the script from another database.
More specifically, I would like to call a stored procedure that will
pass in a new DB name and will take the script that I have created from
another database and simply create a new database with the new name
using the old scripts table structure, etc. It might take reading in
the script and passing in a dynamic variable, but I am having trouble
with it, as everytime I try to create a stored procedure with the
database script in it and substitute the variable @.NEWDBNAME in the
place of each instance of the database name in the script, I get errors
when trying to save the SP.
Any help with this would be hugely appreciated.Hi
Posting your code would have been useful!
You will need to make the whole statement "dynamic" and then execute the
statement.
CREATE PROCEDURE spr_create_new_database
AS
BEGIN
DECLARE @.dbname sysname
DECLARE @.stmt varchar(4000)
/* Create New ISD Backup database dynamically */
SET @.dbname = 'BackupDB_' +
REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16)
,GETDATE(),121),':',''),'-',''),'
','')
SET @.stmt = 'CREATE DATABASE ' + @.dbname
EXEC ( @.stmt )
RETURN
END
If you are going to create this database regularly then you may want to
create a template database and copy the file and use sp_attach_db to create
a
nwe database, alternatively you could use backup/restore. If you want to
create a copy of your live database using RESTORE may be a quicker option
that creating the tables and pulling the data across.
You could use a DTS package to populate the database with a dynamic
properties task and a Copy SQL Server Objects task. This would allow you to
set the destination database without having to change the package.
John
"SQL Server Group 1" wrote:

> I am looking for a way to dynamically create a database in SQL Server
> 2000 using a stored procedure that will create a dynamically named new
> database using the script from another database.
> More specifically, I would like to call a stored procedure that will
> pass in a new DB name and will take the script that I have created from
> another database and simply create a new database with the new name
> using the old scripts table structure, etc. It might take reading in
> the script and passing in a dynamic variable, but I am having trouble
> with it, as everytime I try to create a stored procedure with the
> database script in it and substitute the variable @.NEWDBNAME in the
> place of each instance of the database name in the script, I get errors
> when trying to save the SP.
> Any help with this would be hugely appreciated.
>|||Thanks John for the reply. I am now in the middle of this and need
just a little clarification.
What I did to this point is alter the script to create a database named
"Connect_Template". Then I created the database from the query
analyzer from within the master database and saved the DTS package that
did that. Now I have a database called "Connect_Templage" and a DTS
package called "Create New DB From Termplate".
What I need to do from here is to create a SP that will create a new
database from that DTS package with a new name being passed in. Is
there a way to do that?
In my case, every time a new customer gets created, a new database
based on that template structure needs to be created and I can then
dynamically add a DSN to it from my end. I just need to figure out
what methodis best and will work.
The script to create my database is really really long, but I wonder if
the sp_detach_db and sp_attach_db would work passing in dynamic names?
Thanks again for your help...you have given me a lot of options. I just
need one that would easily work.
I am trying your dynamic statements now but am wondering if the DTP
method would work passing in a dynamic variable.
John Bell wrote:[vbcol=seagreen]
> Hi
> Posting your code would have been useful!
> You will need to make the whole statement "dynamic" and then execute the
> statement.
> CREATE PROCEDURE spr_create_new_database
> AS
> BEGIN
> DECLARE @.dbname sysname
> DECLARE @.stmt varchar(4000)
> /* Create New ISD Backup database dynamically */
> SET @.dbname = 'BackupDB_' +
> REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16)
,GETDATE(),121),':',''),'-',''),'
> ','')
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
> If you are going to create this database regularly then you may want to
> create a template database and copy the file and use sp_attach_db to creat
e a
> nwe database, alternatively you could use backup/restore. If you want to
> create a copy of your live database using RESTORE may be a quicker option
> that creating the tables and pulling the data across.
> You could use a DTS package to populate the database with a dynamic
> properties task and a Copy SQL Server Objects task. This would allow you t
o
> set the destination database without having to change the package.
> John
> "SQL Server Group 1" wrote:
>|||Just for a try, I ran the code below from the Master Database, and it
compiled and worked.
I think that might be the best way to run that. The only thing is now,
how would be the best way to run the create table structures, SPs, etc.
once the new database gets created? How would I create the DTS package
with a dynamic properties task? I have used DTS to import and export
from specific places but not anything dynamically.
Thanks again...this is really helping!
John Bell wrote:[vbcol=seagreen]
> Hi
> Posting your code would have been useful!
> You will need to make the whole statement "dynamic" and then execute the
> statement.
> CREATE PROCEDURE spr_create_new_database
> AS
> BEGIN
> DECLARE @.dbname sysname
> DECLARE @.stmt varchar(4000)
> /* Create New ISD Backup database dynamically */
> SET @.dbname = 'BackupDB_' +
> REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16)
,GETDATE(),121),':',''),'-',''),'
> ','')
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
> If you are going to create this database regularly then you may want to
> create a template database and copy the file and use sp_attach_db to creat
e a
> nwe database, alternatively you could use backup/restore. If you want to
> create a copy of your live database using RESTORE may be a quicker option
> that creating the tables and pulling the data across.
> You could use a DTS package to populate the database with a dynamic
> properties task and a Copy SQL Server Objects task. This would allow you t
o
> set the destination database without having to change the package.
> John
> "SQL Server Group 1" wrote:
>|||Hi
The procedure is fine if you want to create a database with a given name,
but to use that else where you should really create the name and pass it as
a
parameter to the stored procedure. You can then call a DTS package and pass
the database name as a global variable using xp_cmdshell to call DTSRUN see
http://www.sqldts.com/default.aspx?301 about getting help on the command
syntax.
Using this global variable in the DTS package, it can be assigned using the
dynamic properties task can assign it to the destination of a Copy SQL Serve
r
objects task.
e.g.
CREATE PROCEDURE spr_create_new_database ( @.dbname sysname )
AS
BEGIN
DECLARE @.stmt varchar(4000)
/* Create New Backup database dynamically */
SET @.stmt = 'CREATE DATABASE ' + @.dbname
EXEC ( @.stmt )
RETURN
END
Within the job step you can then do something like:
DECLARE @.cmd varchar(4000)
DECLARE @.dbname sysname
SET @.dbname = 'BackupDB_' +
REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16)
,GETDATE(),121),':',''),'-',''),'','
')
EXEC spr_create_new_database @.dbname
SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N "DTS Package" '
SET @.cmd = @.cmd + ' /A " Backup Database Name":"8"="' + @.dbname + '"'
EXEC master..xp_cmdshell @.cmd
This will run the dts package called "DTS Package" on the current server
using trusted authentication, and pass the global variable @.dbname which has
just been used to create the backup database. The global variable is called
"Backup Database Name" You could even pass the source database name as a
global variable and also assign that in the dynamic properties task to make
it totally flexible.
Before you create the DTS package run the stored procedure first so that a
destination database exists for the Copy SQL Objects task. When you create
the DTS Package, create Copy SQL Objects task first and test it, before
creating the global variables and then finally create the Dynamic Properties
task and the workflow to make sure that the Dynamic Properties task runs
before the Copy SQL Objects task.
HTH
John
"SQL Server Group 1" wrote:

> Just for a try, I ran the code below from the Master Database, and it
> compiled and worked.
> I think that might be the best way to run that. The only thing is now,
> how would be the best way to run the create table structures, SPs, etc.
> once the new database gets created? How would I create the DTS package
> with a dynamic properties task? I have used DTS to import and export
> from specific places but not anything dynamically.
> Thanks again...this is really helping!
>
> John Bell wrote:
>|||I really appreciate the help...I am sort of new to the advanced DTS
stuff but have used DTS for import, export. So I will give this a try
and see if I can get it.
Thanks for the link. In creating the Advanced DTS run object, what
would be the dynamic variable name for the database name that is passed
to the copy object? Would it be in the Properties List in the Dynamic
Properties task component under Tasks > Copy SQL Server Objects? For
example: DestinationDatabase, DestinationLogin, DestinationPassword?
If that's right, I think I can do this...
John Bell wrote:[vbcol=seagreen]
> Hi
> The procedure is fine if you want to create a database with a given name,
> but to use that else where you should really create the name and pass it a
s a
> parameter to the stored procedure. You can then call a DTS package and pas
s
> the database name as a global variable using xp_cmdshell to call DTSRUN se
e
> http://www.sqldts.com/default.aspx?301 about getting help on the command
> syntax.
> Using this global variable in the DTS package, it can be assigned using th
e
> dynamic properties task can assign it to the destination of a Copy SQL Ser
ver
> objects task.
> e.g.
> CREATE PROCEDURE spr_create_new_database ( @.dbname sysname )
> AS
> BEGIN
> DECLARE @.stmt varchar(4000)
> /* Create New Backup database dynamically */
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
> Within the job step you can then do something like:
> DECLARE @.cmd varchar(4000)
> DECLARE @.dbname sysname
> SET @.dbname = 'BackupDB_' +
> REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16)
,GETDATE(),121),':',''),'-',''),''
,'')
> EXEC spr_create_new_database @.dbname
> SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N "DTS Package" '
> SET @.cmd = @.cmd + ' /A " Backup Database Name":"8"="' + @.dbname + '"'
> EXEC master..xp_cmdshell @.cmd
> This will run the dts package called "DTS Package" on the current server
> using trusted authentication, and pass the global variable @.dbname which h
as
> just been used to create the backup database. The global variable is calle
d
> "Backup Database Name" You could even pass the source database name as a
> global variable and also assign that in the dynamic properties task to mak
e
> it totally flexible.
> Before you create the DTS package run the stored procedure first so that a
> destination database exists for the Copy SQL Objects task. When you create
> the DTS Package, create Copy SQL Objects task first and test it, before
> creating the global variables and then finally create the Dynamic Properti
es
> task and the workflow to make sure that the Dynamic Properties task runs
> before the Copy SQL Objects task.
> HTH
> John
> "SQL Server Group 1" wrote:
>|||Hi
If you only have the two tasks then the will be called something like
DTSTask_DTSTransferObjectsTask_1
and DTSTask_DTSDynamicPropertiesTask_1
These will be listed under Tasks in the left hand pane of the "Dynamic
Properties Task: Package Properties" dialogue. Choose
DTSTask_DTSTransferObjectsTask_1.
On the right hand side a table of Property Names and Default Values will
appear, double click on DestinationDatabase in the Property Name column and
you will get the Add/Edit Assignment dialogue. Choose Global Variables as th
e
source and your Global variable will be in the name of the drop down.
HTH
John
"SQL Server Group 1" wrote:

> I really appreciate the help...I am sort of new to the advanced DTS
> stuff but have used DTS for import, export. So I will give this a try
> and see if I can get it.
> Thanks for the link. In creating the Advanced DTS run object, what
> would be the dynamic variable name for the database name that is passed
> to the copy object? Would it be in the Properties List in the Dynamic
> Properties task component under Tasks > Copy SQL Server Objects? For
> example: DestinationDatabase, DestinationLogin, DestinationPassword?
> If that's right, I think I can do this...
>
> John Bell wrote:
>|||John,
I almost have it, I think. Here is the stored procedure code I am
running. It created the new database just fine, but it doesn't copy
the data from the template database.
CREATE PROCEDURE sprk_create_new_database ( @.dbname sysname )
AS
BEGIN
DECLARE @.stmt varchar(4000)
/* Create New Backup database dynamically */
SET @.stmt = 'CREATE DATABASE ' + @.dbname
EXEC ( @.stmt )
RETURN
END
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
"Create_New_Connect_DB" '
SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
EXEC master..xp_cmdshell @.cmd
GO
----.
The exec code is:
exec sprk_create_new_database @.dbname = 'TestCopy'
The resulting text is:
The CREATE DATABASE process is allocating 0.63 MB on disk 'TestCopy'.
The CREATE DATABASE process is allocating 0.49 MB on disk
'TestCopy_log'.
However, when it gets to the copy DTS package, "Create_New_Connect_DB",
it does not pass in the variable 'dbname' to the package that has a
Dynamic Properties Variable that passes that name into the
'DestinationDatabase' variable of the Copy SQL Server Objects Task.
Can you see anything here that looks wrong? Again, I really appreciate
the help. I think I'm really close to solving this one!
Brandon|||Hi
You are passing the name to the stored procedure rather than setting the
local variable with the name. The code will not work at all without the loca
l
variable being declared!. This may be easier if you create a procedure to
both call your database creation procedure and the DTS Package (This may wra
p)
CREATE PROCEDURE sprk_create_new_database_and_populate_it
( @.dbname sysname
)
AS
BEGIN
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
"Create_New_Connect_DB" '
SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
EXEC master..xp_cmdshell @.cmd
END
GO
Then run sprk_create_new_database_and_populate_it
passing the name of the
database you wish to populate:
EXEC sprk_create_new_database_and_populate_it
@.dbname = 'TestCopy'
HTH
John
"SQL Server Group 1" wrote:

> John,
> I almost have it, I think. Here is the stored procedure code I am
> running. It created the new database just fine, but it doesn't copy
> the data from the template database.
> CREATE PROCEDURE sprk_create_new_database ( @.dbname sysname )
> AS
> BEGIN
> DECLARE @.stmt varchar(4000)
>
> /* Create New Backup database dynamically */
> SET @.stmt = 'CREATE DATABASE ' + @.dbname
> EXEC ( @.stmt )
> RETURN
> END
>
> DECLARE @.cmd varchar(4000)
> EXEC sprk_create_new_database @.dbname
>
> SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
> "Create_New_Connect_DB" '
> SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
>
> EXEC master..xp_cmdshell @.cmd
> GO
> ----.
> The exec code is:
> exec sprk_create_new_database @.dbname = 'TestCopy'
> The resulting text is:
> The CREATE DATABASE process is allocating 0.63 MB on disk 'TestCopy'.
> The CREATE DATABASE process is allocating 0.49 MB on disk
> 'TestCopy_log'.
> However, when it gets to the copy DTS package, "Create_New_Connect_DB",
> it does not pass in the variable 'dbname' to the package that has a
> Dynamic Properties Variable that passes that name into the
> 'DestinationDatabase' variable of the Copy SQL Server Objects Task.
> Can you see anything here that looks wrong? Again, I really appreciate
> the help. I think I'm really close to solving this one!
> Brandon
>|||OK, here are the two SPs
1) sprk_create_new_database_and_populate_it
CREATE PROCEDURE sprk_create_new_database_and_populate_it
( @.dbname
sysname )
AS
BEGIN
DECLARE @.cmd varchar(4000)
EXEC sprk_create_new_database @.dbname
SET @.cmd = 'DTSRun /S "' + @.@.SERVERNAME + '" /W "0" /E /N
"Create_New_Connect_DB" '
SET @.cmd = @.cmd + ' /A "dbname":"8"="' + @.dbname + '"'
EXEC master..xp_cmdshell @.cmd
END
GO
2) sprk_create_new_database
CREATE PROCEDURE sprk_create_new_database ( @.dbname sysname )
AS
BEGIN
DECLARE @.stmt varchar(4000)
/* Create New Backup database dynamically */
SET @.stmt = 'CREATE DATABASE ' + @.dbname
EXEC ( @.stmt )
RETURN
END
GO
When I run:
EXEC sprk_create_new_database_and_populate_it
@.dbname = 'TestCopy'
I get the message:
DTSRun: Must specify a value for switch: /N The parameter is
incorrect.
DTSRun: Invalid command options
Usage: dtsrun /option [value] [/option [value]] ...
Options ('/?' shows this screen; '-' May be substituted for '/'):
Package retrieval:
/~S Server Name
/~U User Name
/~P Password
/E <Use trusted connection instead of /U /P>
/~N Package Name
/~M Package Password
/~G Package Guid String
/~V Package Version Guid String
/~F Structured Storage UNC filename (overwritten if /S also
specified)
/~R Repository Database Name <uses default if blank; loads package
from repository database>
Package operation (overrides stored Package settings):
/~A Global Variable Name:typeid=Value <may quote entire string
(including name:typeid)>
/~L Log file name
/~W Write Completion Status to Windows Event Log <True or False>
DTSRun action (default is to execute Package):
/!X <Do not execute; retrieves Package to /F filename>
/!D <Do not execute; drop package from SQL Server (cannot drop from
Storage File)>
/!Y <Do not execute; output encrypted command line>
/!C <Copies command line to Windows clipboard (may be used with /!Y
and /!X)>
Notes:
~ is optional; if present, the parameter is hex text of encrypted
value (0x313233...)
Whitespace between command switch and value is optional
Embedded whitespace in values must be embedded in double-quotes
If an option is specified multiple times, the last one wins (except
multiple /A)
Error: -2147024809 (80070057); Provider Error: 0 (0)
Error string: DTSRun: Must specify a value for switch: /N The
parameter is incorrect.
Error source: Microsoft Data Transformation Services (DTS) Package
Help file: sqldts80.hlp
Help context: 0
NULL
Any thoughts...I does created the Database first, which is hopeful.
It just doesn't get through the population part.
Thanks!!