收集的重要的T-SQL语句
来源:互联网 发布:小米盒子直播软件 编辑:程序博客网 时间:2024/05/18 13:05
正在编写升级脚本,这里作个小总结:
(一) 大致SQL的语法:
1. Select
Select 列名1, 列名 as cola, 函数名 as f1
from
{
表名
|
(Select ...) as 别名 --------另名必在from后
Where
字段名=(Select 表达式)
}
有几个要点:
1。 别名在from 后。
2. Select 所带的子表达式,在from 后,用小括号。
3. Where 所带的子表达式,则在"="之后,而且,"="之前,是一个字段名。也就是说,where 的语法格式,是要求一个个三目表达式。与Select 不同。在这里我犯了错。
2. Delete form <表名> where 表达式
比如这个,目的是删除重复项,当然,我得比较二,应该有比这更好的办法。
大至是这样:
1)建一个temp表,用ren进行group 然后,序号写入rowid
2) 从temp中抽取rowid>1的,写入第二个表cu
3) 然后,找到原始表中id与cu表中值相同的行,删除之。
这里的要点是,临时表要在where的表达式中,而不是在where中。
因为,看起来,delete与select不同,delete只能接受一个表(或视图)作为操作参数
--删除当前收货人重复的信息
BEGIN
DELETE FROM shouhuoren
where id = (select id from
(
select * from
(select ren ,id,row_number() over(partition by ren order by id desc) as rowid
from shouhuoren
--制作新的版本的抄送人表
--处理抄送人信息,因为原来的抄送人信息,变成了现在[抄送人-- 项目]对应表,所以,现在我们需要建一个抄送人表
--这样做的目的是,抄送人的信息变成唯一的信息,而不是产多个个重复的抄送人
select * into chaosongren_v2 from (
select row_number() over (order by name)as id,name,email
from (
select *,row_number() over(partition by name order by id desc) as roworder
from dbo.chaosongren
) AS TEMP
where roworder = 1
)a
) tempwhere rowid>1)cuwhere cu.id=shouhuoren.id)
ENDGO
3. Into
select * into : 在表不存在时,把一个表的信息,插入到另一个表中
select * into shouhuoren_v1 from shouhuoren
再演示个复杂的:
--制作新的版本的抄送人表
--处理抄送人信息,因为原来的抄送人信息,变成了现在[抄送人-- 项目]对应表,所以,现在我们需要建一个抄送人表
--这样做的目的是,抄送人的信息变成唯一的信息,而不是产多个个重复的抄送人
select * into chaosongren_v2 from (
select row_number() over (order by name)as id,name,email
from (
select *,row_number() over(partition by name order by id desc) as roworder
from dbo.chaosongren
) AS TEMP
where roworder = 1
)a
4. Update
update RelChaosongrenProject set ChaoSongRenId=dbo.chaosongren_v2.id
from dbo.chaosongren_v2
where dbo.RelChaosongrenProject.name = dbo.chaosongren_v2.name
GO
(二)架构信息的修改
--加入ID列
if not exists(select * from syscolumns where id=object_id('dbo.RelChaosongrenProject') and name='ChaoSongRenId')
BEGIN
ALTER TABLE [dbo].RelChaosongrenProject add ChaoSongRenId int
END
GO
删除一列
ALTER TABLE dbo.RelChaosongrenProject drop COLUMN email
删除表
BEGIN
Declare @Pk varChar(100);
set @Pk='shouhuoren_v2'
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@Pk) AND type in (N'U'))
exec('DROP TABLE '+ @Pk)
END
GO
(三 ) 变量的使用
BEGIN
Declare @Pk varChar(100);
set @Pk='shouhuoren_v2'
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@Pk) AND type in (N'U'))
exec('DROP TABLE '+ @Pk)
END
GO
=============================================
How do I add a auto_increment primary key in SQL Server database?
You need to set the IDENTITY property for "auto number"
ALTER TABLE MyTable ADD mytableID int NOT NULL IDENTITY (1,1) PRIMARY KEY
More precisely to set a named table level constraint
ALTER TABLE MyTable
ADD MytableID int NOT NULL IDENTITY (1,1),
ADD CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (MyTableID)
See ALTER TABLE and IDENTITY on MSDN
(一) 大致SQL的语法:
1. Select
Select 列名1, 列名 as cola, 函数名 as f1
from
{
表名
|
(Select ...) as 别名 --------另名必在from后
Where
字段名=(Select 表达式)
}
有几个要点:
1。 别名在from 后。
2. Select 所带的子表达式,在from 后,用小括号。
3. Where 所带的子表达式,则在"="之后,而且,"="之前,是一个字段名。也就是说,where 的语法格式,是要求一个个三目表达式。与Select 不同。在这里我犯了错。
2. Delete form <表名> where 表达式
比如这个,目的是删除重复项,当然,我得比较二,应该有比这更好的办法。
大至是这样:
1)建一个temp表,用ren进行group 然后,序号写入rowid
2) 从temp中抽取rowid>1的,写入第二个表cu
3) 然后,找到原始表中id与cu表中值相同的行,删除之。
这里的要点是,临时表要在where的表达式中,而不是在where中。
因为,看起来,delete与select不同,delete只能接受一个表(或视图)作为操作参数
--删除当前收货人重复的信息
BEGIN
DELETE FROM shouhuoren
where id = (select id from
(
select * from
(select ren ,id,row_number() over(partition by ren order by id desc) as rowid
from shouhuoren
--制作新的版本的抄送人表
--处理抄送人信息,因为原来的抄送人信息,变成了现在[抄送人-- 项目]对应表,所以,现在我们需要建一个抄送人表
--这样做的目的是,抄送人的信息变成唯一的信息,而不是产多个个重复的抄送人
select * into chaosongren_v2 from (
select row_number() over (order by name)as id,name,email
from (
select *,row_number() over(partition by name order by id desc) as roworder
from dbo.chaosongren
) AS TEMP
where roworder = 1
)a
) tempwhere rowid>1)cuwhere cu.id=shouhuoren.id)
ENDGO
3. Into
select * into : 在表不存在时,把一个表的信息,插入到另一个表中
select * into shouhuoren_v1 from shouhuoren
再演示个复杂的:
--制作新的版本的抄送人表
--处理抄送人信息,因为原来的抄送人信息,变成了现在[抄送人-- 项目]对应表,所以,现在我们需要建一个抄送人表
--这样做的目的是,抄送人的信息变成唯一的信息,而不是产多个个重复的抄送人
select * into chaosongren_v2 from (
select row_number() over (order by name)as id,name,email
from (
select *,row_number() over(partition by name order by id desc) as roworder
from dbo.chaosongren
) AS TEMP
where roworder = 1
)a
4. Update
update RelChaosongrenProject set ChaoSongRenId=dbo.chaosongren_v2.id
from dbo.chaosongren_v2
where dbo.RelChaosongrenProject.name = dbo.chaosongren_v2.name
GO
(二)架构信息的修改
--加入ID列
if not exists(select * from syscolumns where id=object_id('dbo.RelChaosongrenProject') and name='ChaoSongRenId')
BEGIN
ALTER TABLE [dbo].RelChaosongrenProject add ChaoSongRenId int
END
GO
删除一列
ALTER TABLE dbo.RelChaosongrenProject drop COLUMN email
删除表
BEGIN
Declare @Pk varChar(100);
set @Pk='shouhuoren_v2'
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@Pk) AND type in (N'U'))
exec('DROP TABLE '+ @Pk)
END
GO
(三 ) 变量的使用
BEGIN
Declare @Pk varChar(100);
set @Pk='shouhuoren_v2'
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@Pk) AND type in (N'U'))
exec('DROP TABLE '+ @Pk)
END
GO
=============================================
How do I add a auto_increment primary key in SQL Server database?
You need to set the IDENTITY property for "auto number"
ALTER TABLE MyTable ADD mytableID int NOT NULL IDENTITY (1,1) PRIMARY KEY
More precisely to set a named table level constraint
ALTER TABLE MyTable
ADD MytableID int NOT NULL IDENTITY (1,1),
ADD CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (MyTableID)
See ALTER TABLE and IDENTITY on MSDN
- 收集的重要的T-SQL语句
- 收集整理的一些有用的SQL/T-SQL语句!
- 重要实用的sql语句
- 收集的一些SQL语句
- 不熟悉的sql语句收集
- 常用的T-SQL语句
- 收集T-SQL的有用语法
- SQL中比较重要的语句总结
- 自己觉得重要的sql语句
- sql重要的DML和DDL语句
- 个人收藏的SQL语句【T-SQL】
- 经典有用的SQL语句收集
- 【收集】一些常用的SQL语句
- 一些精妙的sql语句收集
- mysql常用的sql语句收集
- 一些比较好的T-SQL语句
- 常用的T-SQL语句和函数
- T-SQL查询语句的执行步骤
- MEMORY
- GPIO_OPEN and CLOSE
- 理想滤波
- static--摘自度娘
- 定时器中断模拟pwm输出声音文件
- 收集的重要的T-SQL语句
- static静态变量的理解 百度
- 构造二叉树并输出
- ESX 4.1 中linux虚拟机磁盘空间(LVM)扩容
- 如何启动bochs
- 六、创建模式之原型(Prototype)模式
- 使用Working Set让eclipse环境看着更清爽
- 数据库优化方案整理
- 关于HSL和HSV颜色空间的详细论述