码迷,mamicode.com
首页 > 数据库 > 详细

SQL通用脚本

时间:2016-02-03 14:29:18      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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通用脚本

标签:

原文地址:http://www.cnblogs.com/quiland/p/SQL.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!