SQL note [SQL judgment exists] [long-term update] (-2015.4)

Source: Internet
Author: User
Tags create index dba

--Determine if a stored procedure exists
if exists(Select* fromsysobjectswhereid = object_id (N' [p_createtable] ') and
OBJECTPROPERTY (ID, N' Isprocedure ') = 1)
Drop procedure[P_createtable]
--Create procP_createtable
-- as
--Create TableTUser (CID nvarchar (), name nvarchar (), ageint, dept nvarchar (50))

--EXEC(' p_createtable ')


--Determine if the database DBA exists
--Create DatabaseDba
if exists(Select* fromsys.databaseswhereName =' DbA ')
Drop Database[DbA]

--Determine if the table TableA exists
--CREATE TABLETableA (ID NVARCHAR (), name NVARCHAR (50))
if exists(Select* fromsysobjectswhereid = object_id (N' [TableA] ') andOBJECTPROPERTY (ID, N' isusertable ') = 1)
Drop Table[TableA]

--Determine if stored procedure p_createtable exists
--Create procP_createtable as
--Create Table User(CID nvarchar (), name nvarchar (), ageint, dept nvarchar (50))
--EXEC(' p_createtable ')
if exists(Select* fromsysobjectswhereid = object_id (N' [p_createtable] ')
andOBJECTPROPERTY (ID, N' Isprocedure ') = 1)
Drop procedure[P_createtable]

--Determine whether the view V_tableab exists
--CREATE VIEWV_tableab as
--SELECTA.id,a.name fromTableA A

IF EXISTS(SELECT* fromSys.viewsWHEREName=' V_tableab ')
DROP VIEWV_tableab--PRINT ' existence '

--Determine if the column column1 exists in the table TableA
--CREATE TABLETableA (ID NVARCHAR (), NAME NVARCHAR (50))--Test data
if exists(Select* fromsyscolumnswhereID=OBJECT_ID (' TableA ') andName=' Column1 ')
Alter TableTableADrop columnColumn1

--Determine if the table TableA has an index Index1
Create IndexIndex1 onTableA (Id,name)--(CREATE INDEX) create a nonclustered index on the ID name two fields

if exists(Select* fromsysindexeswhereID=OBJECT_ID (' TableA ') andName=' index1 ')
--if exists(SelectName fromSys.indexeswhereName = N' Index1 ')
DROP INDEXIndex1 onTableA--Delete index

Select* fromSys.indexeswhereName =' Index1 '--View Index




--Create a temporary table (cached only when executing SQL)
DECLARE@tableATABLE(ID NVARCHAR (), NAME NVARCHAR (50))
INSERT into@tableA (Id,name)VALUES(' AAA ',' BBB ')
SELECT* from@tableA




--Build the library:
------------------------------------------------------------------------
Use Master
Go

if exists(Select* fromsys.databaseswhereName=' Caredb ')
Drop DatabaseCaredb
Go

Create DatabaseCaredb
on(
Name=' Caredb_data ',
Filename=' E:\DB\CareDB_Data.mdf '
)
Log on(
Name=' Caredb_data_log ',
Filename=' E:\DB\CareDB_Data.ldf '
)
Go

--Build table:
------------------------------------------------------------------------
UseCaredb

if exists(Select* fromsysobjectswhereName=' Largedatas ')
Drop TableLargedatas
Go

Create TableLargedatas (
Idvarchar(36),
[FileName]varchar(100),
Content image,
Descriptionsvarchar(100)
)
Go


SQL note [SQL judgment exists] [long-term update] (-2015.4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.