zoukankan      html  css  js  c++  java
  • 关于存储过程设置默认值



    如果要在程序中调用存储过程而又没法给参数传值的话,而sqlserver 存储过程参数没有设置默认值的话,则会抛出异常,可以采用给sqlserver 存储过程设置默认值,如果程序没传入参数值,则使用默认缺省值,这样就不会抛出异常了
    1. SET QUOTED_IDENTIFIER ON 
    2. SET ANSI_NULLS ON 
    3. GO 
    4. ALTER PROC [dbo].[P_Message_user_view] 
    5. --exec P_Message_user_view120904 '20111001','20121002','',null,null,-1,null,12,1,null 
    6. @i_begintime VARCHAR(100) =null , 
    7. @i_endtime VARCHAR(100) =null , 
    8. @i_title VARCHAR(100) =null, 
    9. @i_mtid INT =null,--类型id 
    10. @i_levelid INT =null, --等级id 
    11. @i_depid INT =null, 
    12. @i_userid INT =null, 
    13. @i_pagecount INT =null, 
    14. @i_currentpage INT =null ,     //这些
    15. @i_messageid INT = NULL, 
    16. @i_isclient int = NULL 
    17. AS 
    18. BEGIN 
    19. IF @i_userid IS NULL 
    20. BEGIN 
    21. SET @i_userid=10089 
    22. END 
    23. IF @i_pagecount IS NULL 
    24. BEGIN 
    25. SET @i_pagecount=10000 
    26. END 
    27. IF @i_currentpage IS NULL 
    28. BEGIN 
    29. SET @i_currentpage=10000 
    30. END 
    31. IF @i_begintime IS NULL 
    32. BEGIN 
    33. SET @i_begintime='19000101' 
    34. END 
    35. IF @i_endtime IS NULL 
    36. BEGIN 
    37. SET @i_endtime='20200101' 
    38. END 
    39. IF @i_title IS NULL 
    40. BEGIN 
    41. SET @i_title='' 
    42. END 
    43. DECLARE @v_beginmtid INT,@v_endmtid INT ,@v_begindepid INT ,@v_enddepid INT ,@v_beginlevelid INT, @v_endlevelid INT 
    44. IF @i_mtid IS NULL 
    45. BEGIN 
    46. SET @v_beginmtid=0 
    47. SET @v_endmtid=10000 
    48. END 
    49. ELSE 
    50. BEGIN 
    51. SELECT @v_beginmtid=@i_mtid,@v_endmtid=@i_mtid+1 
    52. END 
    53. IF @i_levelid IS NULL 
    54. BEGIN 
    55. SET @v_beginlevelid=0 
    56. SET @v_endlevelid=10000 
    57. END 
    58. ELSE 
    59. BEGIN 
    60. SELECT @v_beginlevelid=@i_levelid,@v_endlevelid=@i_levelid+1 
    61. END 
    62. CREATE table #dep (depid INT) 
    63. DECLARE @v_sqlstr VARCHAR(8000),@v_sqlstrx VARCHAR(8000),@v_ishot VARCHAR(100) 
    64. set @v_ishot='' 
    65. IF @i_depid IS NULL 
    66. BEGIN 
    67. INSERT INTO #dep 
    68. SELECT id FROM Department d WHERE d.DeletedFlag=0 
    69. END 
    70. IF @i_depid=0 
    71. BEGIN 
    72. INSERT INTO #dep 
    73. SELECT id FROM Department d WHERE d.DeletedFlag=0 
    74. END 
    75. IF @i_depid=-1 
    76. BEGIN 
    77. INSERT INTO #dep 
    78. SELECT id FROM Department d WHERE d.DeletedFlag=0 
    79. SET @v_ishot=' and M.ishot=1' 
    80. END 
    81. IF @i_depid>0 
    82. BEGIN 
    83. DECLARE @v_depcode VARCHAR(100) 
    84. SELECT @v_depcode=depcode 
    85. FROM Department d WHERE id=@i_depid 
    86. INSERT INTO #dep 
    87. SELECT id 
    88. FROM Department
    89. WHERE LEFT(d.depcode,LEN(@v_depcode))=@v_depcode 
    90. END 
    91. CREATE TABLE #count( COUNTs INT) 
    92. SELECT @v_sqlstr=' 
    93. insert into #count 
    94. select count(M.id) from ' 
    95. +tablename +' UT ,Message M,#dep dep 
    96. where UT.userid=' 
    97. +convert(varchar(10),@i_USERID) 
    98. +' 
    99. and UT.messageid=M.id 
    100. and M.deletedflag=0 
    101. and UT.deletedflag=0 
    102. and m.departmentid=dep.depid' 
    103. +@v_ishot+' 
    104. and m.mtid>='+CONVERT(VARCHAR(100),@v_beginmtid)+' 
    105. and m.mtid<'+CONVERT(VARCHAR(100),@v_endmtid)+' 
    106. and m.levelid>='+CONVERT(VARCHAR(100),@v_beginlevelid)+' 
    107. and m.levelid<'+CONVERT(VARCHAR(100),@v_endlevelid)+' 
    108. and m.modifydate>='+''''+@i_begintime+''' 
    109. and m.modifydate<'+''''+@i_endtime+''' 
    110. and m.title like ''%'+@i_title+'%'' 
    111. ',@v_sqlstrx=' 
    112. insert into #showall 
    113. (messageID,MTID,Title,isAttachment,DepartmentID,LevelID,IsRead,ModifyDate,IsHot) 
    114. select top '+CONVERT(varchar(100),@i_pagecount*@i_currentpage)+' 
    115. M.ID,M.MTID,M.Title,M.isAttachment,M.DepartmentID,M.LevelID,UT.IsRead,M.ModifyDate,M.IsHot 
    116. from ' 
    117. +tablename +' UT ,Message M,#dep dep 
    118. where UT.userid=' 
    119. +convert(varchar(10),@i_USERID) 
    120. +' 
    121. and UT.messageid=M.id 
    122. and M.deletedflag=0 
    123. and UT.deletedflag=0' 
    124. +@v_ishot+' 
    125. and m.departmentid=dep.depid 
    126. and m.mtid>='+CONVERT(VARCHAR(100),@v_beginmtid)+' 
    127. and m.mtid<'+CONVERT(VARCHAR(100),@v_endmtid)+' 
    128. and m.levelid>='+CONVERT(VARCHAR(100),@v_beginlevelid)+' 
    129. and m.levelid<'+CONVERT(VARCHAR(100),@v_endlevelid)+' 
    130. and m.modifydate>='+''''+@i_begintime+''' 
    131. and m.modifydate<'+''''+@i_endtime+''' 
    132. and m.title like ''%'+@i_title+'%'''+ 
    133. ' ORDER BY M.ModifyDate DESC 
    134. ' 
    135. FROM ADM_F_GETUSERREADTABLE (@i_USERID) 
    136. --print @v_sqlstr 
    137. --print @v_sqlstr 
    138. EXEC (@v_sqlstr) 
    139. --SELECT * FROM #count 
    140. CREATE TABLE #showall 
    141. ( 
    142. ids INT IDENTITY, 
    143. messageID INT,-- 
    144. MTID INT,-- 
    145. Title VARCHAR(1000), 
    146. isAttachment INT, 
    147. DepartmentID INT, 
    148. LevelID INT, 
    149. IsRead INT, 
    150. ModifyDate DATETIME, 
    151. IsHot int 
    152. ) 
    153. --print @v_sqlstrx 
    154. EXEC (@v_sqlstrx) 
    155. -- SELECT * FROM #showall 
    156. IF @i_messageid IS NULL 
    157. BEGIN 
    158. IF @i_isclient IS NOT NULL 
    159. BEGIN 
    160. SELECT distinct ISNULL(@i_depid,0) AS rootdeptid,c.counts as msgcount, 
    161. --a.ids, 
    162. a.messageID as messageid,a.MTID as mtid, 
    163. CASE WHEN m.needprove = '1' THEN '证' end 
    164. a.Title as title, 
    165. a.isAttachment as isattachment,a.DepartmentID as deptid, 
    166. a.LevelID as levelid,a.IsRead as isread, 
    167. a.ModifyDate as modifydate,a.IsHot as ishot,m.needprove 
    168. FROM #showall a,#count c ,dbo.Message
    169. WHERE m.ID = a.messageID AND a.ids>=(@i_currentpage-1)*@i_pagecount+1 
    170. AND a.ids< (@i_currentpage+1)*@i_pagecount 
    171. order by a.ModifyDate desc 
    172. END 
    173. ELSE 
    174. SELECT distinct ISNULL(@i_depid,0) AS rootdeptid,c.counts as msgcount, 
    175. --a.ids, 
    176. a.messageID as messageid,a.MTID as mtid,a.Title as title, 
    177. a.isAttachment as isattachment,a.DepartmentID as deptid, 
    178. a.LevelID as levelid,a.IsRead as isread, 
    179. a.ModifyDate as modifydate,a.IsHot as ishot,m.needprove 
    180. FROM #showall a,#count c ,dbo.Message
    181. WHERE m.ID = a.messageID AND a.ids>=(@i_currentpage-1)*@i_pagecount+1 
    182. AND a.ids< (@i_currentpage+1)*@i_pagecount 
    183. order by a.ModifyDate desc 
    184. END 
    185. END 
    186. ELSE 
    187. BEGIN 
    188. DECLARE @v_currentids INT,@v_prevmsgid INT,@v_nextmsgid INT 
    189. select @v_currentids=ids FROM #showall WHERE messageID=@i_messageid 
    190. SELECT @v_prevmsgid=messageid FROM #showall WHERE ids=@v_currentids-1 
    191. SELECT @v_nextmsgid=messageid FROM #showall WHERE ids=@v_currentids+1 
    192. SELECT distinct M.*,@v_prevmsgid AS prevmsgid, 
    193. @v_nextmsgid AS nextmsgid, 
    194. shall.IsRead as isread INTO #shw 
    195. FROM MESSAGE M,#showall shall 
    196. WHERE M.id=@i_messageid 
    197. AND m.ID=shall.messageID 
    198. IF EXISTS( SELECT 1 FROM #shw WHERE isread=0) 
    199. BEGIN 
    200. exec [P_Message_User_Update_for_view] @i_userid,@i_messageid 
    201. UPDATE [Message] 
    202. SET Hits =ISNULL(hits,0)+1 
    203. WHERE id=@i_messageid 
    204. END 
    205. SELECT * FROM #shw 
    206. END 
    207. END 
    208. GO 


  • 相关阅读:
    Analysis Services features supported by SQL Server editions
    Azure DevOps to Azure AppServices
    Power BI For Competition
    Win10开机“提示语音”以及”随机播放音乐”
    Azure DevOps
    Allow Only Ajax Requests For An Action In ASP.NET Core
    Mobile CI/CD 101
    Configure SSL for SharePoint 2013
    AWS Step Function Serverless Applications
    Cordova Upload Images using File Transfer Plugin and .Net core WebAPI
  • 原文地址:https://www.cnblogs.com/signheart/p/6595433.html
Copyright © 2011-2022 走看看