一些sql语句的基本用法,以前忽略了啊

来源:互联网 发布:mac 删除重复 图片 编辑:程序博客网 时间:2024/05/16 15:52

Create Table Test

(

Id int identity

,CustomerName nvarchar(50) not null

,Wage int

)

 

having 用法

select CustomerName,count(wage)

from test

group by customerName

having count(wage)>100

 

if exists(select * from Test where Id >10000)
print '123'
else
print '456'

 

//执行sql语句

declare @sql nvarchar(100)

set @sql ='select * from Test'

 

exec sp_executesql @sql

 

--删除重复记录

 delete Test
where id not in(
select min(id)  from test_customer group by customername)

 

--获取每一行的值

SELECT ROW_NUMBER() OVER (ORDER BY EMPID ASCAS ROWID, * FROM EMPLOYEE

--判断一个表是否存在

if object_id(N'UserInfo',N'U') is not null
 print 'b'
else
print 'a'

--判断一个表是否存在一个字段

if  not Exists(Select   *   from   syscolumns   where   id=OBJECT_ID( 'UserInfo')   and   Name= 'ApproveLevel')
 alter table UserInfo add ApproveLevel int