Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2003, 01:43 PM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Saving Queries in Query Analyzer

I'm creating many queries and stored procedures and would like to save them to the database instead of a file or cut and paste, etc.

Am I missing something or is there no way to save a query to anything other than a file in the Query Analyzer tool?

Thanks!d
 
Old July 24th, 2003, 02:18 PM
uit uit is offline
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Greetings -

If you are creating many queries, chances are you're probably going to run them over and over again. So, you may want to consider creating stored procedures. That would be the equivalent of "saving" the queries - as you are asking. In addition, stored procedures offer a lot of flexibility beyond the static query you'd save as a text file - query.sql which you'd load and run time and again.. Check out www.sqlteam.com (this is one of many sites out there) which can help you out with stored procedures, as well as the wrox books. I found SQL Server 2000 Programming a great book...

But, if all you need to do is save the query to the database and run it whenever you want - try this:

1) Incorporate your query as outlined below into a CREATE stored procedure syntax:

Your query:
SELECT <COLUMNS> FROM <TABLENAME> WHERE <CONDITION>

Create Stored Procedure syntax:
CREATE PROCEDURE uspMyStoredProdedure
AS
SELECT <COLUMNS> FROM <TABLENAME> WHERE <CONDITION>
GO

2) Then you can EXECUTE that stored procedure as often as you'd like to:

In query analzyer: EXECUTE MyStoredProcedure


That should get you started....


 
Old July 24th, 2003, 02:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ad hoc queries cannot be saved to the database. (Where would they go?)

Views and stored procedures can be saved by using the CREATE VIEW and CREATE PROCEDURE commands (part of the view or procedure itself). See BOL.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old July 24th, 2003, 02:32 PM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think I see what you mean. I've been starting all my views and procedures with:
   ALTER VIEW..... or CREATE PROCEDURE

By doing this, will the view or procedure be created, altered, etc. each time I run it in the Query Analyzer?

Thanks,
Joe

Quote:
quote:Originally posted by Jeff Mason
 Ad hoc queries cannot be saved to the database. (Where would they go?)

Views and stored procedures can be saved by using the CREATE VIEW and CREATE PROCEDURE commands (part of the view or procedure itself). See BOL.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old July 24th, 2003, 05:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by jscuderi
 I think I see what you mean. I've been starting all my views and procedures with:
ALTER VIEW..... or CREATE PROCEDURE

By doing this, will the view or procedure be created, altered, etc. each time I run it in the Query Analyzer?
Yup. The CREATE/ALTER VIEW/PROCEDURE, is a statement, which when executed by SQL Server, however it is executed (Query Analyzer, EM, via ADO, etc), will create/alter a view/procedure.

Don't include these statements unless you mean to change the object in the database.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Query Analyzer Adam H-W SQL Server 2000 6 August 19th, 2006 04:40 AM
SQL Query Analyzer bmains SQL Server 2000 3 June 23rd, 2004 05:58 PM
Query Analyzer Shortcuts ioates SQL Server 2000 2 February 6th, 2004 05:22 AM
Query Analyzer Walden SQL Server 2000 2 October 28th, 2003 10:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.