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 @sqlStr=@sqlStr + ' 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 @sqlStr=@sqlStr+' m.MembershipCardNo = ''' + @MembershipCardNo +'''';
end
--print @sqlStr
exec sp_executesql @sqlStr
end

GO

你可能感兴趣的:(SQL通用脚本)