Fork me on GitHub

mssql 常用sql 语句

----insert

----delete
----update
----select


----选择数据库进行操作
select top 1 * from smzx2018.dbo.tbuser
use smzx2018;
select top 1 * from tbuser

----基础操作
select COUNT(userid) from smzx2018.dbo.TbUser
select distinct UserType from smzx2018.dbo.TbUser
update ZWOA.dbo.OAClientLogs set EndTime=(select top 1 a.CreateTime from ZWOA.dbo.OAClientLogs a where a.logstatus=30 and a.logtype=15 and a.RelateID=ZWOA.dbo.OAClientLogs.LogID)
select SUM((case when ISNUMERIC(Num)=1 then num else 0 end)) from aaaa

----表联合查询
select '1' as a,'2022-06-10 10:10:10' as b
union
select top 1 username,createtime from tbuser where userid=1
union
select top 1 adminname,CreateDate from AdminInfo where AdminId=1
order by b desc

select a.username,a.truename,b.username,b.phone from smzx2018.dbo.tbuser a,yh_smzx.dbo.tbuser b where a.Phone=b.Phone and a.Phone='13526921550'

----仅备份指定表
select * into BackDb.dbo.ZY_ExportUserInfo from QTHQ.dbo.ZY_ExportUserInfo

---删除数据
delete from smzx2018.dbo.tbuser where userid=1

delete from TbGift where id not in(select max(id) from TbGift Group By UserId)    --删除重复数据


---初始化单表
truncate table smzx2018.dbo.LangeInfo

----存储过程操作
--CREATE proc [dbo].[RARDB]
--ALTER proc [dbo].[RARDB]
--as
--begin
-- exec ('ALTER DATABASE yh_tc SET RECOVERY SIMPLE WITH NO_WAIT;ALTER DATABASE yh_tc SET RECOVERY SIMPLE;USE yh_tc;DBCC SHRINKFILE (N''yh_tc_log'' , 11, TRUNCATEONLY);ALTER DATABASE yh_tc SET RECOVERY FULL WITH NO_WAIT;ALTER DATABASE yh_tc SET RECOVERY FULL;')
-- select 1
--end

----判断字段是否为空
select isnull(NULL,1),isnull('a',1)
select ISNUMERIC(1.11),ISNUMERIC('1.11'),ISNUMERIC('a1.11')

----格式转换
select cast('21123123' as int),cast('2022-06-22' as date),cast('2022-06-22' as datetime),cast('2022-06-22' as time),cast('2022-06-22 22:10:12' as date),cast('2022-06-22 22:10:12' as datetime),cast('2022-06-22 22:10:12' as time)

----计算
select '2022-06-22 00:00:00 '+'09:00',cast('2022-06-22 00:00:00' as datetime)+'09:00',10+'2','2'+10,'a'+cast(1 as nvarchar(50))

----查找字符位置
select charindex(',河南,',',北京,河南,河北,')

--执行SQL语句
exec('select top 1 * from smzx2018.dbo.tbuser')

--定义变量,分组汇总
declare @date varchar(50),@id int,@id1 int,@datetime datetime
set @date='2021-10-01'
----set @id=1
----set @datetime='2022-01-01'
----select @date='2021-10-01',@id=1,@datetime='2022-01-01'
--select @id=userid from yh_smzx.dbo.tbuser where userid=25521
--select @id
--select @id1=isnull((select userid from yh_smzx.dbo.tbuser where userid=1),0)
--select @id1
--************根据日期汇总交易笔数及金额************
select datediff(day,@date,RecTime),dateadd(day,datediff(day,@date,RecTime),@date),count(stasid),sum(MonDeal) from zytk.dbo.ac_PaymentBooks_his where datediff(month,@date,RecTime)=0 group by datediff(day,@date,RecTime) order by datediff(day,@date,RecTime)
--************根据日期汇总交易笔数及金额************


select a1,a2,* from (select powerid a1,powername from PowerInfo where language=10) a,(select powerid a2,powername from powerinfo where language=20) b where b.PowerName like '%'+a.PowerName

posted @ 2022-07-05 10:14  WantRemake  阅读(207)  评论(0编辑  收藏  举报