`
samsongbest
  • 浏览: 167872 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

常用T-sql

 
阅读更多


1.create table
create table dbo.app_param(
sn int identity(1,1) not null,
type nvarchar(50) not null,
module nvarchar(50) not null,
ckey nvarchar(50) not null,
cvalue nvarchar(50) not null,
remark nvarchar(50) null,
updated_user nvarchar(20) null,
created_datetime datetime null,
updated_datetime datetime null,
constraint PK_app_param primary key nonclustered(sn asc)
)


2.create index
create unique clustered index IX_app_param on dbo.app_param
(
type asc,
module asc,
ckey asc
)


3.create foreign key
alter table dbo.app_config add constraint FK_app_config_call_center
foreign key( country_code, call_center_code)
references dbo.call_center(country_code, call_center_code)

4.set null

5. add column
alter table employee add subsidy_status smallint default 1 null
alter table employee_comp_detail add subsidy decimal(16,2) null

6.three common lines
updated_user nvarchar(20) null,
created_datetime datetime default getdate() not null,
updated_datetime datetime default getdate() not null,

7. rename column
exec sp_rename 'kpi_callcenter.active', 'is_active', 'column';

8. change column type
alter table call_center_incentive alter column is_active smallint

9.drop column
alter table employee drop column employee_name_th

10.drop PK
ALTER TABLE dbo.doc_exc DROP CONSTRAINT my_constraint

11.drop index
DROP index anp_comm_rating.IX_anp_comm_rating

7. rename table
exec sp_rename 'table1', 'table2';

8.dump_history_log table

create table if_salespermonth_dump(
employee_id nvarchar(20) null,
month_year nvarchar(20) null,
sales_per_month nvarchar(20) null
)

create table if_salespermonth_history(
sn int identity(1,1) not null,
employee_id nvarchar(20) null,
month_year nvarchar(20) null,
sales_per_month nvarchar(20) null,
created_datetime datetime default getdate() not null,
constraint PK_if_salespermonth_history primary key clustered(sn asc)
)

CREATE TABLE [dbo].if_salespermonth_log(
[sn] [int] IDENTITY(1,1) NOT NULL,
employee_id nvarchar(20) null,
month_year nvarchar(20) null,
sales_per_month nvarchar(20) null,
[transaction_date] [datetime] NOT NULL,
[batch_no] [int] NOT NULL,
[execution_date] [datetime] NOT NULL,
[error_type] [nvarchar](10) NOT NULL,
[error_msg] [nvarchar](200) NULL,
[updated_user] [nvarchar](20) NULL,
[created_datetime] [datetime] default getdate() NOT NULL,
[updated_datetime] [datetime] default getdate() NOT NULL,
CONSTRAINT [PK_if_salespermonth_log] PRIMARY KEY CLUSTERED (sn asc)
)



11.建表的4行
sn int identity(1,1) not null,
updated_user nvarchar(20) null,
created_datetime datetime default getdate() not null,
updated_datetime datetime default getdate() not null,
constraint PK_c_commission_rate primary key nonclustered(sn asc)


13.生成数据字典的sql:select tname,colid,cname,ctype,length =

case ctype
when 'nvarchar' then length/2
when 'nchar' then length/2
else length
end,


xprec,xscale,isnullable from all_col where tname not like 'if%' and tname not like 'cn%'
order by 1,2

select * from all_col order by 1,2


select row_number() over (partition by employee_id order by program_id) as row_num,
*
from employee_program

 

14. 日期加减

/****** Script for SelectTopNRows command from SSMS  ******/

SELECT ''

      , [country_code]

      , [call_center_code]

      , [cutoff_year]- 1

      , [cutoff_month]

      , DATEADD ( year ,- 1, [curr_cutoff_date])

      , [last_cutoff_date]

      , [updated_user]

  FROM [cigna_china_tt]. [dbo]. [cutoff_period]

 

/****** Script for SelectTopNRows command from SSMS  ******/

insert [cutoff_period]( [country_code], [call_center_code], [cutoff_year], [cutoff_month], [curr_cutoff_date],

[last_cutoff_date], [updated_user])

SELECT

      [country_code]

      , [call_center_code]

      , [cutoff_year]- 1

      , [cutoff_month]

      , DATEADD ( year ,- 1, [curr_cutoff_date])

      , DATEADD ( year ,- 1, [last_cutoff_date])

 

      , [updated_user]

  

  FROM [cigna_china_test]. [dbo]. [cutoff_period] where cutoff_year = '2010'

 

  select * from [cutoff_period]

 

  SELECT ''

      , [country_code]

      , [call_center_code]

      , [cutoff_year]- 1

      , [cutoff_month]

      , DATEADD ( year ,- 1, [curr_cutoff_date])

      , [last_cutoff_date]

      , [updated_user]

  FROM [cigna_china_tt]. [dbo]. [cutoff_period]

 

15.fe

DUMP     TRANSACTION     库名      WITH     NO_LOG        

 

16. 收缩日志

ALTER DATABASE ATDB

SET RECOVERY SIMPLE;

GO

-- Shrink the truncated log file to 1 MB.

DBCC SHRINKFILE (ETN_Log, 1);

GO

-- Reset the database recovery model.

ALTER DATABASE ATDB

SET RECOVERY FULL ;

GO

 

17. 行转列

select * from

(

  select country_code, call_center_code, score_type, score, updated_user, updated_datetime

  from kpi_score

) as k pivot ( max ( score) for score_type in( [0], [1], [2])) as t

 

18. convert


  select CONVERT(varchar, getdate(),103)

 

 

 

delete t
            from (select a.seller_id,a.employee_id,
            row_number() over (partition by a.seller_id order by a.seller_id) as r
            from if_employee_info_dump a) t
            where t.r > 1

 

 

 

 

分享到:
评论

相关推荐

    常用T-SQL语句快速参考

    ### 常用T-SQL语句快速参考详解 #### SELECT语句 T-SQL中的SELECT语句用于从一个或多个表中检索数据。其基本语法格式如下: ``` SELECT [ALL|DISTINCT] [TOP n [PERCENT] [WITH TIES]] ``` 其中`ALL`表示返回...

    Inside Microsoft SQL Server 2008 T-SQL Programming.pdf

    - **SQL语句语法**:包括SELECT、INSERT、UPDATE、DELETE等常用语句的使用方法。 - **数据类型**:介绍了SQL Server支持的各种数据类型,如数值类型、字符类型、日期时间类型等。 - **变量与常量**:讲解如何定义...

    SQL、T-SQL与PL-SQL的区别

    SQL、T-SQL与PL-SQL的区别 SQL(Structured Query Language)是...SQL、T-SQL和PL-SQL是关系数据库中的三种常用的语言,每种语言都有其特点和优缺。了解它们之间的区别和特点,对于数据库开发和管理具有重要的意义。

    T-SQL示例大全(全是T-SQL语句 )

    以上只是T-SQL部分核心概念和常用语句的概述,实际的“T-SQL示例大全”中会有更详细、具体的实例,包括但不限于这些内容。通过学习和实践这些示例,你可以深入了解T-SQL的强大功能,并提升数据库管理技能。

    触发器使用原理和常用T-SQL截取字符串操作

    它们是数据库层面的程序,通常用T-SQL(Transact-SQL)编写,这是SQL Server的扩展语法。 例如,"触发器的使用案例.txt"可能包含以下内容:创建一个触发器来确保每当在员工表中更新薪水时,同时更新部门的总预算。...

    数据库基础知识(经典的常用T-sql语句)

    本文将介绍数据库的基础知识,特别是T-SQL语言中的常见语句,这些语句包括查询(Query)、增加(Insert)、删除(Delete)、修改(Update)等操作。 首先,连接到数据库的命令通常使用MySQL的格式,例如`mysql -h...

    常用T-sql,查询,关联

    本资料主要涵盖的是T-SQL在查询和关联方面的常用技巧。 一、基础查询 1. SELECT语句:T-SQL中最基础的查询语句,用于从表中选取数据。例如,`SELECT * FROM TableName` 可以获取表中的所有数据。 2. WHERE子句:...

    《T-SQL 2008 入门》[PDF]

    在T-SQL的进阶部分,读者将接触到存储过程和函数的创建与调用,这在实际开发中非常常用。存储过程可以封装复杂的逻辑,提高代码复用性,而函数则能返回单一值,便于在查询中使用。此外,触发器的概念也会被讲解,它...

    T-SQL学习资料(CSDN下载)集合

    "T-SQL语句简介.wmv" 是一段视频教程,可能以生动直观的方式介绍T-SQL的基础概念和常用语句,对于初学者来说,通过观看演示和实例来学习会更加直观易懂。 总之,这份T-SQL学习资料集合提供了一个全面的学习路径,从...

    SQLServer数据库管理常用的SQL和T-SQL语句

    SQL Server 数据库管理常用的 SQL 和 T-SQL 语句 SQL Server 是一个强大的关系数据库管理系统,提供了许多实用的 SQL 和 T-SQL 语句来管理和维护数据库。以下是 SQL Server 数据库管理常用的 SQL 和 T-SQL 语句: ...

    T-SQL程序设计与游标设计

    T-SQL 程序设计是数据库系统原理实验三的主要内容之一,它包括变量、流程控制命令、其他命令和常用函数四部分。 变量是 T-SQL 程序设计中的重要组成部分,有两种变量:局部变量和全局变量。局部变量是用户自行定义...

    SQL SERVER实用教程 T-SQL语言.pdf

    通过上述内容的详细介绍,读者可以了解到SQL语言的基础概念、T-SQL语言的具体构成以及常用的数据类型和常量。这对于学习和掌握SQL Server数据库的使用具有重要的指导意义。在实际的应用场景中,掌握这些基本知识可以...

    17.函数整理(T-SQL 版).pdf

    文档的其余部分可能还包含有关这些函数更多细节和示例,但根据提供的内容来看,涵盖了T-SQL中常用函数的基本知识。这些函数对于进行数据操作和数据处理是十分关键的,是数据库开发者和数据库管理人员必须掌握的基础...

    T-sql 语言大全

    【T-SQL 语言大全】 T-SQL,全称 Transact-SQL,是SQL语言的一个扩展,主要用于微软的SQL Server数据库管理系统中。它是SQL标准的一个实现,但包含了额外的功能和特有语法,使得开发者能够更加灵活地管理和操作...

    T-SQL编程入门(SQL Server)

    ### T-SQL编程入门(SQL Server) #### 一、T-SQL的组成 T-SQL (Transact-SQL) 是 Microsoft SQL Server 使用的一种扩展版本的 SQL 语言,它提供了额外的功能来增强 SQL 的功能,使开发者能够更高效地管理和操作...

Global site tag (gtag.js) - Google Analytics