SQL通用脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL通用脚本相关的知识,希望对你有一定的参考价值。
1. 还原数据时,剔除占用:
ALTER DATABASE Golf7_JHW SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER database Golf7_JHW set online
2. 查询表字段说明SQL:
SELECT objname,value FROM ::fn_listextendedproperty (NULL, ‘user‘, ‘dbo‘, ‘table‘, ‘表名‘, ‘column‘, default)
4. 查询重复数据
例如:SELECT * FROM dbo.T_Club WHERE ClubCode IN (SELECT ClubCode FROM dbo.T_Club GROUP BY ClubCode HAVING COUNT(*) > 1)
5. 查询出现重复数据时只显示一条
例如:SELECT * FROM dbo.T_Club WHERE ClubID IN (SELECT MAX(ClubID) FROM dbo.T_Club GROUP BY ClubCode)
3. 动态SQL脚本(SP):
--=======================================================
--Created By: yzh
--Created Date: 2015-12-31
--Description: 查看会员权益
--Test: exec P_GetEquityPlayNum ‘F2015‘,11
--=======================================================
ALTER proc [dbo].[P_GetEquityPlayNum]
(
@MembershipCardNo varchar(200)
,@ClubID int
)
as
begin
declare @sqlStr nvarchar(max)
set @sqlStr=‘
select
ClubID
,RegionInnerWeekdayNumber
,RegionInnerHolidayNumber
,LimitRegionInnerNumber
,RegionOuterWeekdayNumber
,RegionOuterHolidayNumber
,LimitRegionOuterNumber
from T_EquityPlayNum e
inner join T_MemberShip m on e.MemberType = m.MembershipTypeID ‘
--球会ID
if not(@ClubID is null or @ClubID=0)
begin
set @sqlStr = @sqlStr + case when charindex(‘WHERE‘,@sqlStr) > 0 then ‘ AND‘ else ‘ WHERE‘ end;
set @[email protected] + ‘ e.ClubID = ‘ + Convert(varchar,@ClubID);
end
--会籍证号
if not(@MembershipCardNo is null or @MembershipCardNo=‘‘)
begin
set @sqlStr = @sqlStr+ case when charindex(‘WHERE‘,@sqlStr) > 0 then ‘ AND‘ else ‘ WHERE‘ end;
set @[email protected]+‘ m.MembershipCardNo = ‘‘‘ + @MembershipCardNo +‘‘‘‘;
end
--print @sqlStr
exec sp_executesql @sqlStr
end
GO
以上是关于SQL通用脚本的主要内容,如果未能解决你的问题,请参考以下文章