-- 1. 创建表 ID 自动增长
create table His_PoolStoreGoods(
ID int identity(1,1) primary key,
sStoreNO varchar(4) not null,
nGoodsID numeric(10,0) not null,
nTag int not null,
sPoolNO varchar(20) not null,
dConfirmDate datetime not null,
dCreateDate datetime not null
)
-- 2. 增加列
alter table His_PoolStoreGoods add ID numeric(12,0)
-- 3. 创建索引
create unique index idx_tMonthSaleAmount
on tMonthSaleAmount(sStoreNO,nSmallCategoryID,dTradeDate)
--- 4. 循环执行存储过程(循环时间)
begin tran
declare @r datetime='2019-06-02 00:00:00.000'
delete from tCategoryDaily where dTradeDate>=@r
declare @rend datetime='2019-07-03 00:00:00.000'
while(@r<=@rend)
begin
exec rj_CategoryDaily @r
set @r=dateadd(day,1,@r)
end
commit
-- 5. 查看有没有事务在数据库执行
SELECT es.session_id, es.login_name, es.host_name, est.text
, cn.last_read, cn.last_write, es.program_name
FROM sys.dm_exec_sessions es
INNER JOIN sys.dm_tran_session_transactions st --系统里还存在的事务
ON es.session_id = st.session_id
INNER JOIN sys.dm_exec_connections cn
ON es.session_id = cn.session_id
CROSS APPLY sys.dm_exec_sql_text(cn.most_recent_sql_handle) est
LEFT OUTER JOIN sys.dm_exec_requests er
ON st.session_id = er.session_id
-- 6. 日结列表过程
select * from tRunList
-- 7. 查询数据ID
select DB_ID ('BX_DEV_FRMDB') -- “BX_DEV_FRMDB” 为数据库名
-- 8. 随机取两条数据
select top 2 * from spkfk order by newid()
--9. 查看过程的
select * from sys.objects where type='P'
and name like '%Prom%'
-- 10 .行列转换
SELECT year as 年份, Q1 as 一季度, Q2 as 二季度, Q3 as 三季度, Q4 as 四季度
FROM SalesByQuarter PIVOT (SUM (amount) FOR quarter IN (Q1, Q2, Q3, Q4) ) AS P
ORDER BY YEAR DESC