Forum Moderators: coopster & phranque

Message Too Old, No Replies

MS SQL syntax reference?

need suggestions

         

theEntropy

5:13 pm on Mar 21, 2002 (gmt 0)



Can anyone suggest a good *free* online SQL syntax reference?

I am writing a stored procedure that will export data from one table to another but not quite sure the right way to go about it. .

also. . when writing queries for mySql you can test for the existence of a table (DROP TABLE IF EXISTS tablename;) but this syntax does not fly in MS SQL.. . is there a way to do this when writing for MS SQL?

TIA

toadhall

5:25 pm on Mar 21, 2002 (gmt 0)

10+ Year Member



I think your best bet is to simply use your subject as a SE search phrase, "sql syntax reference".

hasbeen

5:28 pm on Mar 21, 2002 (gmt 0)

10+ Year Member



Welcome theEntropy...

I believe the proper MSSQL syntax would be:

IF EXISTS(SELECT TABLE_NAME FROM DBNAME
WHERE TABLE_NAME = 'table_name')
DROP TABLE table_name

As for reference material, I'll second toadhall's recommendation of searching for SQL references. There are too many good ones to list here.

theEntropy

5:31 pm on Mar 21, 2002 (gmt 0)



been there done that.. .

I have books too. .

But as Bono put it. . "I still haven't found what I'm looking for".. .

toadhall

5:35 pm on Mar 21, 2002 (gmt 0)

10+ Year Member



Try searching under "structured query language" as well.
More 'history' pages there, and tutorials, reference, etc.

theEntropy

5:58 pm on Mar 21, 2002 (gmt 0)



good idea, toadhall, definately got better results that way, thanks. .

the syntax checks out for your suggestion on the conditional statement,
hasbeen, thanks. .

just gotta get it to execute now. . doesn't seem to like anything after 'FROM'.. .

ppg

10:12 am on Mar 22, 2002 (gmt 0)

10+ Year Member



The syntax: DROP TABLE IF EXISTS tablename is right. Perhaps you don't have the right permissions to drop the table?

For copying data from one table to another:

INSERT INTO destination_table SELECT * FROM source_table;

or to create the table at the same time:

CREATE TABLE new_table SELECT * FROM source_table;

hope this helps

mattur

12:39 pm on Mar 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The SQL Server 2000 docs are available as a [url="http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp"]free download[/url] on the Microsoft site.

It's a 35MB CAB file...

theEntropy

8:52 pm on Mar 22, 2002 (gmt 0)



-PPG - The "DROP TABLE IF EXISTS table_name;" code will not work with MS SQL. . only in mySql from what i have seen.. also the "CREATE TABLE new_table SELECT * FROM source_table;" errored out at 'SELECT'.. perhaps that, too, only works with mySql. . .
*HOWEVER*, the "INSERT INTO destination_table SELECT * FROM source_table;" code worked famously for my purposes!!

thanks PPG!

-mattur - would that be the same as BooksOnline, documentation for MS SQL Server? or is it documentation on *writing* Structured Query Language for MS SQL?

thanks all. .

E

JuniorHarris

5:30 pm on Mar 28, 2002 (gmt 0)

10+ Year Member



The following MS SQL Server syntax can be used to copy and create a new table:

select * into new_table from exisiting_table

To drop a table try:

if exists (select * from sysobjects where name = 'drop_table')
drop table drop_table

Where drop_table is the name of the table to be dropped.