sqlserver 基本操作
修改字段属性
alter table 表名 alter column 字段名 varchar(1000)
修改表名称
EXEC sp_rename '原表名', '新表名'
修改表字段名称
EXEC sp_rename 'tablename.oldfield', 'newfield', 'COLUMN'
取出group by 的第一条记录(必须有不同的id)
select * from table where id in (select min(id) from table group by name)
将一个表中的数据插入到另一个表中
1.如果目标表不存在:
select * into 目标表 from 旧表
2 如果目标表存在
insert into 目标表 select * from 旧表
删除带有默认值的列
declare @name varchar(150) //最好足够大 否则会截断
select @name=b.name from syscolumns a,sysobjects b
where a.id=object_id('[表名]') and b.id=a.cdefault and a.name='[列名]' and b.name
like 'DF%'
exec('alter table article drop constraint '+@name)
alter
table [表名] drop column [列名]
获取带有重复内容
select id, name, memo from Awhere id in (select id from A group by id having count(1) >= 2)
like 区分大小写查询:
select * from tablename where filed COLLATE Latin1_General_CS_AS like '%X%'
选出某列值为数字的行:
select * from 表 where isnumeric(字段)=1
按in顺序查出数据:
select * from ibs6_terminal_adv_inf where id in (16,14,15) order by CHARINDEX(','+ltrim(id)+',',',16,14,15,')
sqlserver取随机数
select cast(floor(rand(checksum(newid()))*N) as int)
N为你要的随机数的范围即0-N
创建数据库链接(详细链接http://blog.csdn.net/ninetowns2008/article/details/7034531)
exec sp_addlinkedserver '192.168.180.1','','SQLOLEDB','192.168.180.167'
exec sp_addlinkedsrvlogin '192.168.180.1','false','用户名','用户名','密码'