SQL 实战教程(八)

http://www.studyofnet.com/news/247.html

1.修改字段为自增

alter table [dbo].[Logs] drop column ID

 alter table [dbo].[Logs] add Id int identity(1,1)

2.添加默认字段

alter table EA ADD EAInvoiceType int NOT NULL Default 1

alter table EA ADD TaxMoney float NOT NULL Default 0

 ALTER TABLE [dbo].[ProductCategorys]  ADD IsEnable  int default 0

通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数 增加字段: alter table [表名] add 字段名 smallint default 0 增加数字字段,整型,缺省值为0 alter table [表名] add 字段名 int default 0 增加数字字段,长整型,缺省值为0 alter table [表名] add 字段名 single default 0 增加数字字段,单精度型,缺省值为0 alter table [表名] add 字段名 double default 0 增加数字字段,双精度型,缺省值为0 alter table [表名] add 字段名 Tinyint default 0 增加数字字段,字节型,缺省值为0

alter table [表名] add 字段名 text [null] 增加备注型字段,[null]可选参数 alter table [表名] add 字段名 memo [null] 增加备注型字段,[null]可选参数

alter table [表名] add 字段名 varchar(N) [null] 增加变长文本型字段 大小 为N(1~255) alter table [表名] add 字段名 char [null] 增加定长文本型字段 大小固定为255

alter table [表名] add 字段名 Datetime default 函数 增加日期型字段,其中 函数 可以是 now(),date()等,表示缺省值 (上面都是最常用的,还有其他的属性,可以参考下面的数据类型描述)

删除字段: alter table [表名] drop 字段名

修改变长文本型字段的大小:alter table [表名] alter 字段名 varchar(N)

删除表: drop table [表名]

创建表: sql="CREATE TABLE [表名] ([字段1,并设置为主键] int IDENTITY (1, 1) NOT NULL CONSTRAINT PrimaryKey PRIMARY KEY,"&_ "[字段2] varchar(50),"&_ "[字段3] single default 0,"&_ "[字段4] varchar(100) null,"&_ "[字段5] smallint default 0,"&_ "[字段6] int default 0,"&_ "[字段7] date default date(),"&_ "[字段8] int default 1)" conn.execute sql

有null 的表示字段允许零长

4.去重

select da.OpenId,cf.Code,da.orgNo,da.OrgName,da.ReallyName,da.telephone,da.Email,da.Province,da.City,da.CreateTime from (
select cu.Id,cr.OpenId,cu.orgNo,cu.OrgName,cu.ReallyName,cu.telephone,cu.Email,cu.Province,cu.City,cu.CreateTime from (
select DISTINCT(cp.OpenId),max(cp.CreateTime) as CTime from [dbo].[CouponCodeInfoes] co  left join [dbo].[CouponUsers] cp on co.[CouponUserId]=cp.Id
where cp.CreateTime>'2016-09-30'  group by cp.OpenId ) cr left join [dbo].[CouponUsers] cu on cr.CTime=cu.CreateTime and cr.OpenId=cu.OpenId
) da left join [dbo].[CouponCodeInfoes]  cf on da.Id=cf.[CouponUserId]

去重2

select  wx.OpenId, wx.[PrizeType],wx.Note,wx.payStatus,tr.orgNo,tr.OrgName,tr.ReallyName,tr.telephone,tr.Email,tr.Province,tr.City,wx.CreateTime from  [dbo].[WxUserPrizes] wx left  join
(
select cr.OpenId,cr.CuTime,cu.orgNo,cu.OrgName,cu.ReallyName,cu.telephone,cu.Email,cu.Province,cu.City,cu.CreateTime from (
select OpenId,max(Createtime) as CuTime from [dbo].[CouponUsers] group by OpenId
) cr left join [dbo].[CouponUsers] cu on cr.OpenId=cu.OpenId and cr.CuTime=cu.CreateTime
)  tr  on wx.OpenId=tr.OpenId where wx.[PrizeType]=2 order by wx.CreateTime

一对多,左查询

 select Project.ProjectID,max(Invoice.CreateTime) as  InvoiceTime from Project ,Invoice
 where Project.ProjectID=Invoice.ProjectID group by Project.ProjectID

5.Incorrect syntax near the keyword 'user'.

SQL 查询特殊字字符串:

UPDATE   [dbo].[APIKeys]  SET  [Key]='AC20C39A-7AEB-4354-A7A9-15D102043855' 

6.GDI+中发生一般性错误的解决办法

1. 相应的帐户没有写权限。
解决方法:赋予 NETWORK SERVICE 帐户以写权限。
2. 指定的物理路径不存在。
解决方法:
在调用 Save 方法之前,先判断目录是否存在,若不存在,则创建。
if (!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
3. 保存的文件已存在并因某种原因被锁定

删除指定列:

 alter table [dbo].[RequestUsers] drop column AgreeContent

添加列


alter table [dbo].[CreditDetails] add CreditTypeID  uniqueidentifier default '4F5E5643-4649-4D25-8B6A-F644A76B4291' not null

4.分组处理


select  JudgeUserId,sum(Distinct(StepGroupNum)) as count from [dbo].[RaceStepJudges] group by  JudgeUserId,StepGroupNum

select JudgeUserId,count(JudgeUserId) as ct from (
select  JudgeUserId,StepGroupNum as GPCount from [dbo].[RaceStepJudges]  where RaceStepConfigId='3AFF201F-A07A-4E83-BD21-408DC7FF9687' group by  JudgeUserId,StepGroupNum
) stInfo  group by  JudgeUserId

5.select  导出字段分割

select substring(id,1,3) +','+substring(id,4,3) as id from test

SELECT Address, PARSENAME(REPLACE([Address],'-','.'),4) as 小区名,
--如果字段的内容是 4单元-12幢-203 那么此时小区名字段的信息就是NULL
PARSENAME(REPLACE([Address],'-','.'),3) as 单元号,
PARSENAME(REPLACE([Address],'-','.'),2) as 楼房号,
PARSENAME(REPLACE([Address],'-','.'),1) as 房间号
FROM Person

利用excel 间隔符号导出数据源

 

posted @ 2017-08-11 18:12  李寒星  阅读(319)  评论(0编辑  收藏  举报