473,387 Members | 1,420 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,387 software developers and data experts.

Basic Questions

Hi All,

I am used to other SQL engines, and have a few basic questions--

1)If I wanted to conditionally drop a table, does SQL Server have a
way to natively do this? Many SQL implementations will allow
something like:

CREATE OR REPLACE tablename AS
SELECT
x,y,z
FROM sourcetable
;

Does SQL Server have something like this? This syntax, both the
"create table as select" syntax and the "create or replace" syntax
seem to cause problems.

2) Some of our existing queries have a keyword, "GO" where I would
otherwise expect a semi-colon. Is there a functional difference
between the two? I seem to be able to replace the "GO" keywords with
semi-colons without any changes in how the script behaves, but I
thought I would check and see if anyone has advice about the
differences here.

Thanks in advance,
Ben

Feb 13 '07 #1
6 4738
BJMurphy (mu********@gmail.com) writes:
I am used to other SQL engines, and have a few basic questions--

1)If I wanted to conditionally drop a table, does SQL Server have a
way to natively do this? Many SQL implementations will allow
something like:

CREATE OR REPLACE tablename AS
SELECT
x,y,z
FROM sourcetable
;

Does SQL Server have something like this? This syntax, both the
"create table as select" syntax and the "create or replace" syntax
seem to cause problems.
You can create a table from a query this way:

SELET x, y, z
INTO tablename
FROM sourcetable

There is nothing resemblent of CREATE OR REPLACE for anything in SQL
Server. You need to have things like:

IF object_id('sometable') IS NOT NULL
DROP TABLE sometable
2) Some of our existing queries have a keyword, "GO" where I would
otherwise expect a semi-colon. Is there a functional difference
between the two? I seem to be able to replace the "GO" keywords with
semi-colons without any changes in how the script behaves, but I
thought I would check and see if anyone has advice about the
differences here.
GO and ; are entirely unrelated. In T-SQL, GO is just another
identifier like MyTable, [Order Details] or whatever. GO is used by
many query tools as a batch separator, so that you can give the tool
something like:

CREATE something
go
-- Use this something
go
-- Drop this something

Each batch will be sent to SQL Server separately. A batch can consist
of many statements, separated or not by semicolons.

In application code you would typically never use GO, but each Execute
command is a batch.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 13 '07 #2
On Feb 13, 5:17 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
BJMurphy (murphy....@gmail.com) writes:
I am used to other SQL engines, and have a few basic questions--
1)If I wanted to conditionally drop a table, does SQL Server have a
way to natively do this? Many SQL implementations will allow
something like:
CREATE OR REPLACE tablename AS
SELECT
x,y,z
FROM sourcetable
;
Does SQL Server have something like this? This syntax, both the
"create table as select" syntax and the "create or replace" syntax
seem to cause problems.

You can create a table from a query this way:

SELET x, y, z
INTO tablename
FROM sourcetable

There is nothing resemblent of CREATE OR REPLACE for anything in SQL
Server. You need to have things like:

IF object_id('sometable') IS NOT NULL
DROP TABLE sometable
2) Some of our existing queries have a keyword, "GO" where I would
otherwise expect a semi-colon. Is there a functional difference
between the two? I seem to be able to replace the "GO" keywords with
semi-colons without any changes in how the script behaves, but I
thought I would check and see if anyone has advice about the
differences here.

GO and ; are entirely unrelated. In T-SQL, GO is just another
identifier like MyTable, [Order Details] or whatever. GO is used by
many query tools as a batch separator, so that you can give the tool
something like:

CREATE something
go
-- Use this something
go
-- Drop this something

Each batch will be sent to SQL Server separately. A batch can consist
of many statements, separated or not by semicolons.

In application code you would typically never use GO, but each Execute
command is a batch.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx- Hide quoted text -

- Show quoted text -
Thanks, Erland, for your response.

To clarify further, if I use a semi-colon at the end of my query, and
put several queries in a row, SQL server will not execute them until
it reaches the end of the set of queries or a "GO" statement. Is that
correct?

Put another way, if I am creating table A in one query and then want
to use it in the next query, do I have any options other than using
"GO" after the first query to ensure that table A will be available in
the second query?

I understood the semi-colon to be analogous to "GO" in that both would
submit the query to the SQL server, acting as a separator, but it
seems that you are saying this is not the case. I apologize for my
confusion, I just want to make sure I have everything straight in my
head.

Feb 14 '07 #3
I'm not the expert that Erland is, but I think I know.

GO and semicolon both serve to separate statements. But GO also
separates batches.

If you make 4 statements into one batch like this --

statement1; statement2; statement3; statement4 GO

....and statement4 fails with an error, the affected objects are
restored to the way they were before statement1.

If you make them 4 separate batches like this --

statement1 GO statement2 GO statement3 GO statement4 GO

.... and statement4 fails with an error, the objects are restored to
the way they were after statement 3.

Jim
Feb 14 '07 #4
On Feb 14, 4:54 pm, jim_geiss...@countrywide.com wrote:
I'm not the expert that Erland is, but I think I know.

GO and semicolon both serve to separate statements. But GO also
separates batches.

If you make 4 statements into one batch like this --

statement1; statement2; statement3; statement4 GO

...and statement4 fails with an error, the affected objects are
restored to the way they were before statement1.

If you make them 4 separate batches like this --

statement1 GO statement2 GO statement3 GO statement4 GO

... and statement4 fails with an error, the objects are restored to
the way they were after statement 3.

Jim
OK. It seems as though the variables that I had setup were only valid
and were local to the specific batch in which they were created. If I
have ten statements, some of which are codependent, but I know they
will run successfully, it seems like I would want to use semi-colons
because it will allow me to keep the values of the local variables
throughout the entire batch, since there is only one batch. Would the
codependent queries still work?

For example:

create table foo;
select * from bar into foo;
select * from foo where (true);

Is then equivalent to:
create table foo
go
select * from bar into foo
go
select * from foo where (true);

Again, assuming the queries run succesfully.

Is there a way to create global variables?

Thanks for your response, Jim. I appreciate the help. Also, don't
worry, a book is on the way (the highly recommended one that comes in
two parts, I forget the title and author, sorry), so I won't be
posting too many questions, hopefullly :-)

Feb 14 '07 #5
BJMurphy (mu********@gmail.com) writes:
To clarify further, if I use a semi-colon at the end of my query, and
put several queries in a row, SQL server will not execute them until
it reaches the end of the set of queries or a "GO" statement. Is that
correct?
Not really. SQL Server never reaches any GO statement. GO is not a statement
at all. GO carries no particular meaning for SQL Server at all. GO is a
separator that is intercepted by interactive query tools. You would
never have any GO in a program.

Keep in mind that SQL Server is a client-server application. Query
Analyzer or SQL Server Management Studio are not SQL Server. They
are client tools. When you select a couple of statements in a
query window, they will send these to SQL Server. But if there are
GO on some lines, they will send the batches one by one. And one
batch will not be sent, until the previous has executed.

Once in SQL Server, SQL Server will execute the statements, sequentially.
(Unless there is some control-of-flow languages.)

The semi-colons are statement terminators, but in difference to
other semicolon languages, they are optional in SQL Server. (Except in
a few situations where they are needed to disambiguiate the grammar.)
Put another way, if I am creating table A in one query and then want
to use it in the next query, do I have any options other than using
"GO" after the first query to ensure that table A will be available in
the second query?
There are statements that must be alone in a batck, like CREATE PROCEDURE
and CREATE VIEW. So to refer to you newly created view, you need to
do this in a separate batch. There are also some commands that does not
take effect until the next batch. Some SET commands are like that.

However, CREATE TABLE does not have any of these quirks. You can say:

CREATE #mytable (a int NOT NULL
INSERT #mytable(a) VALUES (213)

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 14 '07 #6
BJMurphy (mu********@gmail.com) writes:
For example:

create table foo;
select * from bar into foo;
select * from foo where (true);

Is then equivalent to:
create table foo
go
select * from bar into foo
go
select * from foo where (true);

Again, assuming the queries run succesfully.
Well, since two of them has syntax errors, they will not. So
the first batch will fail. But if you run the second set of bathces,
the second statement will create the table "foo".
Is there a way to create global variables?
No. The place to write data that needs to be persisted over batches
is in tables, typically temp tables.

(OK, there is a command SET CONTEXT_INFO which could be used for
this, but it's not anywhere close to practical for this purpose.)

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 14 '07 #7

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

Similar topics

2
by: AK | last post by:
I don't want any part of the previous discussion on Visual Basic versus Visual Basic.Net. My query is about using Visual Basic for Applications; and whether it is better to use Visual Basic 6 or...
8
by: Orange Free | last post by:
I want to create a program that will ask a user a series of questions and then generate a Microsoft Word document whose content is dictated by the answers. I am not a professional programmer, and...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
2
by: Steven O. | last post by:
First, this may not be the correct newsgroup. I have some relatively basic questions on SQL. I tried to find a newsgroup that was specifically just about SQL, and was surprised to find that all...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
3
by: Jim H | last post by:
If there is a site someone can point me to that answers such basic questions, rather than taking up support's time posting answers, please let me know. I've developed C# .NET, unmanaged C++, and...
2
by: Fay Yocum | last post by:
BEWARE beginner questions!! I have some experience in Access but never as much as I want or need. I have decided to get in on VB.Net. I would only rate myself in Access as a...
0
by: software2006 | last post by:
ASP And Visual Basic Interview questions and answers I have listed over 100 ASP and Visual Basic interview questions and answers in my website...
4
by: Goran Djuranovic | last post by:
Hi all, I am experiencing a strange thing happening with a "designer.vb" page. Controls I manually declare in this page are automatically deleted after I drop another control on a ".aspx" page. -...
2
by: LayneMitch via WebmasterKB.com | last post by:
Hello. This is a basic quiz. Only about 3 questions. I'm having issue with the answer key which stays on a certain value regardless of acknowledging that you have the correct answer for the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.