473,411 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Query Analyzer Script

I am trying to conditionally create a stored procedure in a Query
Analyzer script. When I have the CREATE PROCEDURE statement by itself
in the script, it compiles and runs ok. Once I put a condition on the
script, it does not run.

I have tried wrapping the script inside a BEGIN...END but it does not
help.

ex:
IF (SELECT [Version] FROM [tblDefaults]) < '5.11a'
CREATE PROCEDURE [dbo].[SelectByAccount]

@ARAcct int
AS

SELECT * FROM tblTable WHERE [AccountNo] = @Acct

This does not work.

I have also tried wrapping the entire CREATE PROCEDURE statement in an
EXEC() but that does not work when I have literals in the select
statement.

HELP!!
Thanks
Paul
Jul 20 '05 #1
2 4397

"Paul Sieloff" <si*****@artronix-solutions.com> wrote in message
news:a6**************************@posting.google.c om...
I am trying to conditionally create a stored procedure in a Query
Analyzer script. When I have the CREATE PROCEDURE statement by itself
in the script, it compiles and runs ok. Once I put a condition on the
script, it does not run.

I have tried wrapping the script inside a BEGIN...END but it does not
help.

ex:
IF (SELECT [Version] FROM [tblDefaults]) < '5.11a'
CREATE PROCEDURE [dbo].[SelectByAccount]

@ARAcct int
AS

SELECT * FROM tblTable WHERE [AccountNo] = @Acct

This does not work.

I have also tried wrapping the entire CREATE PROCEDURE statement in an
EXEC() but that does not work when I have literals in the select
statement.

HELP!!
Thanks
Paul


CREATE PROCEDURE must be the only statement in the batch, so you can't
conditionally create one with IF. You can use EXEC() to do this, though, and
I guess that what you mean by 'does not work' is that you haven't doubled up
the quotes around values in your code (see example below).

You don't give any information about why you're trying to do this, but if
it's part of a deployment or migration script, you might find it easier to
use a client tool which connects to the database, then conditionally creates
the objects.

Finally, you may want to review your version logic - using the syntax above,
version '5.9a' is greater than '5.11a' and version '10.11a' is less than it,
based on string comparison.

Simon

if (select Version from dbo.tblDefaults) < '5.11a'
exec ('
create procedure dbo.MyProc
as
select col1 from dbo.MyTable where col2 = ''A''
')
Jul 20 '05 #2
Paul Sieloff (si*****@artronix-solutions.com) writes:
I am trying to conditionally create a stored procedure in a Query
Analyzer script. When I have the CREATE PROCEDURE statement by itself
in the script, it compiles and runs ok. Once I put a condition on the
script, it does not run.

I have tried wrapping the script inside a BEGIN...END but it does not
help.

ex:
IF (SELECT [Version] FROM [tblDefaults]) < '5.11a'
CREATE PROCEDURE [dbo].[SelectByAccount]

@ARAcct int
AS

SELECT * FROM tblTable WHERE [AccountNo] = @Acct

This does not work.

I have also tried wrapping the entire CREATE PROCEDURE statement in an
EXEC() but that does not work when I have literals in the select
statement.


The CREATE PROCEDURE must be the first in the batch, why this does
not work.

In short, trying to run all in SQL is a very tediuos affair, as you
need to embed all code in EXEC() and that.

It's better to write a program in your favourite client language:
VBscript, Perl or even a BAT file, and have the client program to
do the controlling logic.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Warren Wright | last post by:
Hi group, I have a select statement that if run against a 1 million record database directly in query analyzer takes less than 1 second. However, if I execute the select statement in a stored...
2
by: Poster | last post by:
After creating an IN clause from a bunch of character strings created by a Word macro, Query Analyzer complains about a syntax error. The macro takes a column full of character strings and wraps...
3
by: JM | last post by:
Good day. I was able to connect to a database server using SQL Server Enterprise Manager. The Server name specified on the tree is JOMARGON(Windows NT). But no server was detected using either...
1
by: MichaelC | last post by:
Hi all, I'm a newbie to SQL and I need help with investigating what ways are possible to build an interface of some sort that wraps around a SQL query script. I have a simple SQL query which...
1
by: justinjoylife | last post by:
Hi - I'm completely new to Microsoft Query Analyzer and I need to learn it for work to do data mining as a Product Manager. Does anyone have any recommendations on how to learn this and where?...
1
by: Crash | last post by:
Hi, ..NET v1.x SP1 VS 2003 SQL Server 2000 SP3 Server 2000, XP, Server 2003 I would like to programmatically execute {possibly many} SQL Server batch scripts. Aka I have many scripts that...
3
by: Matthew Wells | last post by:
Hello Is there a way to use Goto or something like it in QA? I have a script with a few GO statements, as I'm sure you know, declared variables, labels, and goto's only have scope within each...
2
by: SQL Server Questions | last post by:
Environment: Server1 (Local) OS Windows 2000 Server SQL Server 2000 Server2 (Remote) OS Windows 2003 Server SQL Server 2000 (Both with most recent service packs)
1
by: cryogeneric | last post by:
Hello! My company recently upgraded to SQL Server 2005 and I'm still getting used to the new interface. One thing I've noticed: When I'm connected to a specific database and I want to run a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.