Groups | Blog | Home
all groups > sql server (microsoft) > january 2007 >

sql server (microsoft) : how to check if mytable is exist via an asp file



Mint
1/12/2007 10:02:29 PM
i want to create an asp script.
it can do the task below.
1. check if the mytable is exist?
2. if exists drop mytable
3. create my mytable.
my question how to check if mytable is exist?
please advice an asp script.
or anyone can tell me how to create a procedure which can create a
table , the table will be a paremeter , and i can run call the
procedure in an asp file.
many thanks.
sux_stellino
1/13/2007 3:31:50 AM
Hi!

I suggest you to use a stored procedure like this:

CREATE PROCEDURE proc_CreateTable
(
@TableToCreate nvarchar(30)
)
AS
BEGIN

SET NOCOUNT ON;

DECLARE @SQLTable nvarchar(1000)

-- create drop table script
SET @SQLTable = 'DROP TABLE ' + @TableToCreate

-- check if the table exists. In that case, drop table with
sp_executeSQL
IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(@TableToCreate) AND type in (N'U'))
EXEC sp_ExecuteSQL @SQLTable

-- create script for create table
SET @SQLTable = 'CREATE TABLE ' + @TableToCreate + '
(
id int identity(1,1) primary key clustered,
description varchar(100)
)'

-- execute the create table script
EXEC sp_ExecuteSQL @SQLTable
END

It is valid for SQL Server 2000 and 2005. You can improve this script
adding a schema/owner check. In sql server 2005 you can check it into a
system view called sys.schemas.

All that you have to do in asp it's to create an ado command and
execute it as a Storedprocedure command, adding also a parameter called
@TableToCreate into parameters collection of command object.

That's all..
hope this help..
bye!

[quoted text, click to view]
AddThis Social Bookmark Button