Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"create if not exist" and "create table like" sql server query

how can i create a table just if that table is not exist. in my case, i want to create table with this query :

SELECT * 
INTO a
FROM b

that table a in db A and table b in db B. any help?

like image 331
Chaos Ryeze Avatar asked Dec 25 '22 23:12

Chaos Ryeze


2 Answers

if not exists (select [name] from sys.tables where [name] = 'a') SELECT * 
INTO A.dbo.a
FROM B.dbo.b

you can try this .. its simple one.

like image 200
Krichevskoy Avatar answered May 12 '23 05:05

Krichevskoy


You can use the OBJECT_ID function for SQL Server.

IF OBJECT_ID('a') IS NULL
        SELECT *
        INTO a
        FROM b
like image 44
Steven Wexler Avatar answered May 12 '23 06:05

Steven Wexler