Showing posts with label creates. Show all posts
Showing posts with label creates. Show all posts

Thursday, March 29, 2012

Create Table within an IF statement causes error

This doesn't make any sense to me. I am trying to create a stored procedure that creates a temp table using T-SQL. The table will be created differently depending on the arguments passed. Here is an example of what I am trying to do:

DECLARE @.Switch bit

SET @.Switch = 0

IF @.Switch = 0

BEGIN

PRINT @.Switch

CREATE TABLE #DontWork (Zero int)

END

ELSE

BEGIN

PRINT @.Switch

CREATE TABLE #DontWork (One int)

END

SELECT * FROM #DontWork

If you run this as is, it fails stating that "There is already an object named '#DontWork' in the database." However if you comment out one of the CREATE TABLE statements (either one of them), it works fine. The PRINT @.Switch line will prove that the IF ELSE statement is evaluating properly if you change the value of @.Switch. My guess is that the parsing engine is throwing the error before it even tries to run the code. Is there any way to make this work the way it should? Or do I have to resort to creating 2 different tables and modifying the rest of my code to compensate for the change?

This is usually caused because you have ran a CREATE TABLE statement in a previous development iteration. Try appending this to the end of your code:

Code Snippet

go

drop table #DontWork

The temp table stays in scope after you run through one time so the next time through you get the error. Try hiliting the code I've given you and execute just the DROP TABLE. Then un-hilite the code and rerun query. It should run correctly once you have dropped the table.

OK, I'm all wet... Hang on.

You can alter it:

Code Snippet

create table #what (one int)

alter table #what
add two int

alter table #what
drop column one

select * from #what

go

drop table #what

/*
two
--
*/

|||

The code doesn't execute.

The parsing engine is attempting to resolve the objects, and (incorrectly, in my opinion) assumes that the second instance of the create table is attempting to make a second object with the same name. The parsing engine is resolving objects, not checking logic and code flow.

To test, comment out EITHER CREATE statement and the code executes.

Your options include creating the #Temp table before the IF statement, or using a different #Table name in the second instance.

Or you could have both switched locations call out to another procedure that creates the #Temp table.

|||My code already involves altering the table. I was just trying to use an IF statement because one scenario creates a predictable table structure, and the other side requires that the field names be calculated at run time. I was trying to save myself some effort by simply having that CREATE TABLE command in there twice, but it seems that because of what Arnie said about the parsing engine resolving objects, not checking logic and code flow, I'm going to have to do things the complicated way. I wish there was a way to communicate things like this to the powers that be at Microsoft. Any idea how to do that, if at all possible?|||

They do pay attention to the suggestions.

Suggestions for SQL Server

http://connect.microsoft.com/sqlserver

|||MS SQL has always had a problem with this construct. The solution, as mentioned, is to create the table once, and then use alter table to change the table to what you want. Or just create 2 tables of different names.

|||

SQL Server compiles the entire batch (SP, trigger, function or ad-hoc) and compilation doesn't take into account run-time information (variable values, control of flow etc). This gets tricky for temporary tables because of the way they are scoped. For best performance and manageability, you should put the creation logic for the different conditions in their own SPs and the execution logic too. This provides better reusability. You could use the ALTER TABLE approach but that will give bad performance in SQL Server 2005 since it negates the caching that we do automatically on temporary tables (metadata & 1 page of allocation which can get reused). Of course, if you can remove the temporary tables altogether.

Btw, your code will work if you were creating a permanent table conditionally.

Wednesday, March 21, 2012

Create SQL install script

I want to create a SQL script like the one for Northwind... that creates the DB with data (instnwnd.sql).

I have the DB running I just don't know how to add all the tables and content into a file.

In mySQL I would just use phpmy admin to create the scipt... just don't know how in MS SQL.

Any help is greatly appreciated.

If you got an existing database to can just right click the mouse in MS SQL 2005 and generate the sql script, you can put in in the clipboard or on a script.sql file.

Do you have MS SQL 2005? 2000 or express?

|||

I have 2005... I have tried to do that but I only get the tables and not the data.

Thanks!

|||

AH!

If you want the data, you'll have to Export and you can still select a file or another database.

|||

Doh... I was in SQL Express. I also have SQL 2005. I know see it...

Thanks!

|||

Well, it's Saturday, minds are a little confused on SaturdaysWink

|||

Here is article that describes the way it could be made.http://www.denovations.com/articles/

Unfortunately there is no standard way to do it.

Sunday, March 11, 2012

Create procedure in target servers

I am creating a job in master server where in one step, it creates
stored procedure in target server. The proc text is exceeding the
limit to directly paste in job scheduler. What is the best way to push
procedure to target servers?You can either split the sproc into smaller ones to bypass the text size
limit, or save the proc in a text file and use osql in the job to call the
input file.
"tram" <tram_e@.hotmail.com> wrote in message
news:26ee1067.0407130929.62a38b86@.posting.google.com...
> I am creating a job in master server where in one step, it creates
> stored procedure in target server. The proc text is exceeding the
> limit to directly paste in job scheduler. What is the best way to push
> procedure to target servers?|||Thanks for the reply. OSQL could be used, but I need to copy the sql
to every server. It doesn't take if it is located at central server.
Any ideas?
"Richard Ding" <rding@.acadian-asset.com> wrote in message news:<eJ6GcnQaEHA.3664@.TK2MSFTNGP12.phx.gbl>...
> You can either split the sproc into smaller ones to bypass the text size
> limit, or save the proc in a text file and use osql in the job to call the
> input file.
>
> "tram" <tram_e@.hotmail.com> wrote in message
> news:26ee1067.0407130929.62a38b86@.posting.google.com...
> > I am creating a job in master server where in one step, it creates
> > stored procedure in target server. The proc text is exceeding the
> > limit to directly paste in job scheduler. What is the best way to push
> > procedure to target servers?|||Thanks for the reply. OSQL could be used, but I need to copy the sql
to every server. It doesn't take if it is located at central server.
Any ideas?
"Richard Ding" <rding@.acadian-asset.com> wrote in message news:<eJ6GcnQaEHA.3664@.TK2MSFTNGP12.phx.gbl>...
> You can either split the sproc into smaller ones to bypass the text size
> limit, or save the proc in a text file and use osql in the job to call the
> input file.
>
> "tram" <tram_e@.hotmail.com> wrote in message
> news:26ee1067.0407130929.62a38b86@.posting.google.com...
> > I am creating a job in master server where in one step, it creates
> > stored procedure in target server. The proc text is exceeding the
> > limit to directly paste in job scheduler. What is the best way to push
> > procedure to target servers?|||Thanks for the reply. OSQL could be used, but I need to copy the sql
to every server. It doesn't take if it is located at central server.
Any ideas?
"Richard Ding" <rding@.acadian-asset.com> wrote in message news:<eJ6GcnQaEHA.3664@.TK2MSFTNGP12.phx.gbl>...
> You can either split the sproc into smaller ones to bypass the text size
> limit, or save the proc in a text file and use osql in the job to call the
> input file.
>
> "tram" <tram_e@.hotmail.com> wrote in message
> news:26ee1067.0407130929.62a38b86@.posting.google.com...
> > I am creating a job in master server where in one step, it creates
> > stored procedure in target server. The proc text is exceeding the
> > limit to directly paste in job scheduler. What is the best way to push
> > procedure to target servers?

CREATE PROC Question

Gurus help me:
Here's the scenario...
Have a SP in the Master DB that creates a NEW, empty DB using a name I give it on the fly.
I need to Create a SP in that NEW DB.
Everything will be called from a DTS Package.
How to do this?
RobbieDCan you tell us why you are doing this...

Just seems like a very bad idea...

Are you talking about MSDE?|||I'll second the notion that this sounds like a bad idea. It can certainly be done, but there are lots of things that you can do, but shouldn't!

-PatP|||"Location: In front of the computer"

LOL

Moe, Larry look, it's a DBA with a sense of humor...

Why I oughtta...|||Hey Brett:

It's to automate Replication (see my other posts).

We have a subjective DB name @. the Publisher that has to be acquired, then replicated EXACTLY.

This procedure will create the Subscription DB & then create the SP to complete the the subscription itself.

Clear as Mud?!?!

(BTW - GREAT reply to the recruiter. He suddenly became less verbose!)|||Sounds ambitious...how many subscribers do you expect to have...|||Just a single other instance...But we'll do this MONTHLY.

HOWEVER, we have to duplicate the process in reverse later on.

Ambitious pretty much hits the nail on the head!!!|||I wouldn't support that kind of design, but here's your answer:

use model
go
create procedure <your_procedure>...
go|||Just 1?

That's a lot of effort to think outside the box...why complicate things?|||Got any suggestions?|||Did you get it or I have to explain it?|||Sorry rdjabarov:

I see where you're going, but if I want this code in a SP OR for that matter in an ActiveX module of a DTS, I can't get away with "USE".|||But your only replicating monthly?

Why not dump and restore?

MAYBE 10 lines of code

Done!|||robbied111,

You don't call this code from anywhere, you write it in QA. Since you already have the code to create a database, you won't have to worry about creating a procedure every time your ASP code creates a database, the procedure will already be there...Can you try it at least?|||THANKS All.

I'll do some more work & let you know how I fare.

RobbieD

(It's past 5pm here - time to blaze!!!)

Wednesday, March 7, 2012

create multiple stored proc thru script

Hi,

I am using an MSDE database.
During installation, i intend to use a a script file which creates all tables and stored procedures using ADODB in InstallShield.
But i get the error
"CREATE PROCEDURE must be the first statement in a batch update". Moreover "GO" is not recognized by ADODB.
How can i create the stored procedures ?

regards,
henryCall osql to execute the script?|||Hi Brett,

I am using osql to execute the script successfully. But i am still wondering how to do this through ADODB.

Thanks and regards,
henry

Friday, February 17, 2012

CREATE DATABASE script doesnt accept a variable for a FILENAME

I want to create a SP that creates a new database, so I script-ed out the db and paste the script into new SP gui (SQL 2000). I want to pass it a variable for the data/log file location that is not the default location. The original script looks like this:

CREATE DATABASE [PWRR_DDS] ON (NAME = N'PWRR_DDS_Data', FILENAME = N'F:\SQL SERVER FILES\Databases\dbName.mdf' , SIZE = 3118, FILEGROWTH = 10%) LOG ON (NAME = N'PWRR_DDS_Log', FILENAME = N'F:\SQL SERVER FILES\Databases\dbName_Log.LDF' , SIZE = 5000, FILEGROWTH = 10%) COLLATE SQL_Latin1_General_CP1_CI_AS.

I replaced the path of the FILENAME variable like this:


CREATE DATABASE [PWRR_DDS] ON (NAME = N'PWRR_DDS_Data', FILENAME = @.DBPath, SIZE = 5000, FILEGROWTH = 10%) LOG ON (NAME = N'PWRR_DDS_Log', FILENAME = @.LogPath , SIZE = 5000, FILEGROWTH = 10%) COLLATE SQL_Latin1_General_CP1_CI_AS,

declaring the variables as char(500). The error I get is "Incorrect sybtax near'@.DBPath'.

Any ideas for workaround?

Thanks,

EJM

You could build the stmt and EXEC it.

declare @.sqlnvarchar(500)set @.sql ='CREATE DATABASE [' + @.dbname + +'] ON (NAME = ' + @.datafile +', FILENAME = ' + @.DBPath +', SIZE = 5000, FILEGROWTH = 10%) LOG ON (NAME = ' + @.log +', FILENAME = ' + @.LogPath +', SIZE = 5000, FILEGROWTH = 10%) COLLATE SQL_Latin1_General_CP1_CI_AS..'EXEC(@.sql)

Create Database Script - Help Required

Hi,

I have a Database.sql script file that creates my application database (Malibu). My question is how do I get around the problem of the hard coded FILENAME as shown below...


CREATE DATABASE [Malibu] ON (NAME = N'Malibu_Data', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Malibu_Data.MDF' , SIZE = 4, FILEGROWTH = 10%) LOG ON (NAME = N'Malibu_Log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Malibu_Log.LDF' , SIZE = 1, FILEGROWTH = 10%)

If this path does not exist on the clients PC - the script will fail.

Any suggestions appreciated.

Thanks.

Steve.Build the script as a string and then execute the string.

Declare @.strScript as varchar(5000)

SET @.strScript = 'CREATE DATABASE [Malibu] FILENAME = ' + @.thecorrectpathhereasavariable + '\Malibu_Data.MDF' '

Exec(@.strScript)

How you actually determine the path I do not know, but you can variableize the script as the above example.|||Thanks Wes,

I currently run the script from a batch file using OSQL, however, the way things are heading with .Net, I guess I should look at doing it in VB code.

Steve.|||The code I posted was T-SQL code.