当表/存储过程/视图/触发器存在时,将其删除。。。
IF OBJECT_ID(N'表名/存储过程名/视图/触发器名',N'U/P/V/TR') IS NOT NULL
DROP TABLE/PROCEDURE/VIEW/TRIGGER 表名/存储过程名/视图/触发器名

IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME = '表名/存储过程名/视图/触发器名' AND TYPE = 'U/P/V/TR')
DROP TABLE/PROCEDURE/VIEW/TRIGGER 表名/存储过程名/视图/触发器名


select * into 目的库.dbo.表名 from 表名

select * into 目的库.dbo.表名 from 表名 where 1=1 复制表结构与内容
select * into 目的库.dbo.表名 from 表名 where 1=0 只复制表结构

@@IDENTITY --返回最后插入的标识值。


说明:两张关联表,删除主表中已经在副表中没有的信息
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

----------------------------------------------------------------------------
表结构如下
id     name         age
 1   zhangsan    100
 2   lisi                200
 3   wangwu      100
 4   zhaoliou      500
 5   qianqi          500
 6   werwer         245
 7   jopio            786
要求:
1.写一个 sql语句,目的是获得相同age的人的name

select a.[name],a.[age] from [table] a inner join
(
select age from [table] 
group by age 
having(count(age)>1)
) b
on a.[age] = b.[age]

select [name],[age] from [table]
where [age] in
(
 select age from [table]  group by [age] having(count([age]) > 1)
)

2.将name中含有'i'的字符替换成'*'

select replace(name,'i','*') from [table]

posted on 2007-07-24 12:23  秦岭过客  阅读(514)  评论(0编辑  收藏  举报