Java教程

Sql 常用语句

本文主要是介绍Sql 常用语句,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
--删除重复数据   
--(重复全备份)
select att.* into RnsAttachmentsDeleteAllBack0810 from RnsAttachments att
inner join                              
(select t.ForeignId,t.ProjectForeignId from RnsAttachments t
where t.ProjectForeignId is not null
group by t.ForeignId,t.ProjectForeignId having count(1)>1) tt
on att.ForeignId= tt.ForeignId and att.ProjectForeignId = tt.ProjectForeignId;

--重复数据只留一条
select ProjectForeignId,ForeignId,max(Id)maxid into RnsAttachmentsNotDeleteId from
RnsAttachmentsDeleteAllBack0810  group by ProjectForeignId,ForeignId ;

--删除不留下来的数据。
delete del from RnsAttachments del 
inner join RnsAttachmentsDeleteAllBack0810 Delsearch
on del.id=Delsearch.id 
left join RnsAttachmentsNotDeleteId noDel
on del.id=noDel.maxid 
where noDel.maxid is  null;
处理重复数据
SELECT  ( CASE WHEN a.colorder = 1 THEN d.name
               ELSE ''
          END ) AS 表名 ,--如果表名相同就返回空  
        a.colorder AS 字段序号 ,
        a.name AS 字段名 ,
        ( CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '√'
               ELSE ''
          END ) AS 标识 ,
        ( CASE WHEN ( SELECT    COUNT(*)
                      FROM      sysobjects--查询主键  
                      WHERE     ( name IN (
                                  SELECT    name
                                  FROM      sysindexes
                                  WHERE     ( id = a.id )
                                            AND ( indid IN (
                                                  SELECT    indid
                                                  FROM      sysindexkeys
                                                  WHERE     ( id = a.id )
                                                            AND ( colid IN (
                                                              SELECT
                                                              colid
                                                              FROM
                                                              syscolumns
                                                              WHERE
                                                              ( id = a.id )
                                                              AND ( name = a.name ) ) ) ) ) ) )
                                AND ( xtype = 'PK' )
                    ) > 0 THEN '√'
               ELSE ''
          END ) AS 主键 ,--查询主键END  
        b.name AS 类型 ,
        a.length AS 占用字节数 ,
        COLUMNPROPERTY(a.id, a.name, 'PRECISION') AS 长度 ,
        ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数 ,
        ( CASE WHEN a.isnullable = 1 THEN '√'
               ELSE ''
          END ) AS 允许空 ,
        ISNULL(e.text, '') AS 默认值 ,
        ISNULL(g.[value], '') AS 字段说明
FROM    syscolumns a
        LEFT JOIN systypes b ON a.xtype = b.xusertype
        INNER JOIN sysobjects d ON a.id = d.id
                                   AND d.xtype = 'U'
                                   AND d.name <> 'dtproperties'
        LEFT JOIN syscomments e ON a.cdefault = e.id
        LEFT JOIN sys.extended_properties g ON a.id = g.major_id
                                               AND a.colid = g.minor_id
        where d.name in('RnsEmployees','RnsProjects','RnsProjectRelatives','RnsProjectCompanyRelatives','RnsCompanies','RnsJobMapping')
ORDER BY a.id ,
        a.colorder;
表字段说明
        select 
  ROW_NUMBER() OVER (ORDER BY a.object_id) AS No, 
  a.name AS 表名,
  isnull(g.[value],'-') AS 说明
from
  sys.tables a left join sys.extended_properties g
  on (a.object_id = g.major_id AND g.minor_id = 0);
表说明
--增加表说明
EXECUTE sp_addextendedproperty   N'MS_Description','表说明',N'user',N'dbo',N'table',N'表名',NULL,NULL
EXECUTE sp_updateextendedproperty N'MS_Description','项目类型',N'user',N'dbo',N'table',N'ProjectType',NULL,NULL;

--增加字段
ALTER TABLE 表名 ADD    字段名 int not NULL default 8;
--增加字段注释
EXEC sp_addextendedproperty 'MS_Description', '规格型号日志id22', 'SCHEMA', dbo, 'table', LOG_REV_SPEC, 'column', logid;
--修改字段注释
EXEC sp_addextendedproperty 'MS_Description', '规格型号日志id22', 'SCHEMA', dbo, 'table', LOG_REV_SPEC, 'column', logid;
表结构修改

 

这篇关于Sql 常用语句的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!