SQL Server的一些操作:
1、判断一个表是否存在:
if exists(select * from sysobjects where id=OBJECT_ID(N'[' + @tableName + ']') and OBJECTPROPERTY(id,N'IsUserTable') = 1)
相对于这个数据库来说,OBJECT_ID根据表名得到ID,N表示显示的将非Unicode字符转化为Unicode字符,来自SQL-92 标准中的 National(Unicode)数据类型。OBJECTPROPERTY:返回当前数据库中对象的有关信息。1表“真”。
2、int转为Char
@char = cast(@int as varchar)
3、使用游标
--定义游标declare MyCursor Cursor forselect CompanyId from dbo.Company where CompanyID in (7,29,16)--打开游标open MyCursor--循环游标declare @index intfetch next from MyCursor into @indexwhile @@FETCH_STATUS=0beginexec('drop table dbo.C' + @index + '_DisplayPattern')fetch next from MyCursor into @indexend--关闭游标close MyCursor--释放资源deallocate MyCursor
4、选择前几条,并且把相同的留下
select TOP 4 with ties * from dbo.T_Personorder by FAge
5、SQL的控制符列表
--ms sql里的控制字符列表:
--Tab char(9)--换行 char(10)--回车 char(13)--单引号 char(39)--双引号 char(34)
6、得到一个表的行数
set @sqls='select @a=COUNT(*) from [DemoSite_0213_OldCSV].[dbo].[C' + @company + '_Proposal]'exec sp_executesql @sqls,N'@a int output',@oldNumber output