如果要在程序中调用存储过程而又没法给参数传值的话,而sqlserver 存储过程参数没有设置默认值的话,则会抛出异常,可以采用给sqlserver 存储过程设置默认值,如果程序没传入参数值,则使用默认缺省值,这样就不会抛出异常了
SET QUOTED_IDENTIFIER ONSET ANSI_NULLS ONGOALTER PROC [dbo].[P_Message_user_view]--exec P_Message_user_view120904 '20111001','20121002','',null,null,-1,null,12,1,null@i_begintime VARCHAR(100) =null ,@i_endtime VARCHAR(100) =null ,@i_title VARCHAR(100) =null,@i_mtid INT =null,--类型id@i_levelid INT =null, --等级id@i_depid INT =null,@i_userid INT =null,@i_pagecount INT =null,@i_currentpage INT =null , //这些@i_messageid INT = NULL,@i_isclient int = NULLASBEGINIF @i_userid IS NULLBEGINSET @i_userid=10089ENDIF @i_pagecount IS NULLBEGINSET @i_pagecount=10000ENDIF @i_currentpage IS NULLBEGINSET @i_currentpage=10000ENDIF @i_begintime IS NULLBEGINSET @i_begintime='19000101'ENDIF @i_endtime IS NULLBEGINSET @i_endtime='20200101'ENDIF @i_title IS NULLBEGINSET @i_title=''ENDDECLARE @v_beginmtid INT,@v_endmtid INT ,@v_begindepid INT ,@v_enddepid INT ,@v_beginlevelid INT, @v_endlevelid INTIF @i_mtid IS NULLBEGINSET @v_beginmtid=0SET @v_endmtid=10000ENDELSEBEGINSELECT @v_beginmtid=@i_mtid,@v_endmtid=@i_mtid+1ENDIF @i_levelid IS NULLBEGINSET @v_beginlevelid=0SET @v_endlevelid=10000ENDELSEBEGINSELECT @v_beginlevelid=@i_levelid,@v_endlevelid=@i_levelid+1ENDCREATE table #dep (depid INT)DECLARE @v_sqlstr VARCHAR(8000),@v_sqlstrx VARCHAR(8000),@v_ishot VARCHAR(100)set @v_ishot=''IF @i_depid IS NULLBEGININSERT INTO #depSELECT id FROM Department d WHERE d.DeletedFlag=0ENDIF @i_depid=0BEGININSERT INTO #depSELECT id FROM Department d WHERE d.DeletedFlag=0ENDIF @i_depid=-1BEGININSERT INTO #depSELECT id FROM Department d WHERE d.DeletedFlag=0SET @v_ishot=' and M.ishot=1'ENDIF @i_depid>0BEGINDECLARE @v_depcode VARCHAR(100)SELECT @v_depcode=depcodeFROM Department d WHERE id=@i_depidINSERT INTO #depSELECT idFROM Department dWHERE LEFT(d.depcode,LEN(@v_depcode))=@v_depcodeENDCREATE TABLE #count( COUNTs INT)SELECT @v_sqlstr='insert into #countselect count(M.id) from '+tablename +' UT ,Message M,#dep depwhere UT.userid='+convert(varchar(10),@i_USERID)+'and UT.messageid=M.idand M.deletedflag=0and UT.deletedflag=0and m.departmentid=dep.depid'+@v_ishot+'and m.mtid>='+CONVERT(VARCHAR(100),@v_beginmtid)+'and m.mtid<'+CONVERT(VARCHAR(100),@v_endmtid)+'and m.levelid>='+CONVERT(VARCHAR(100),@v_beginlevelid)+'and m.levelid<'+CONVERT(VARCHAR(100),@v_endlevelid)+'and m.modifydate>='+''''+@i_begintime+'''and m.modifydate<'+''''+@i_endtime+'''and m.title like ''%'+@i_title+'%''',@v_sqlstrx='insert into #showall(messageID,MTID,Title,isAttachment,DepartmentID,LevelID,IsRead,ModifyDate,IsHot)select top '+CONVERT(varchar(100),@i_pagecount*@i_currentpage)+'M.ID,M.MTID,M.Title,M.isAttachment,M.DepartmentID,M.LevelID,UT.IsRead,M.ModifyDate,M.IsHotfrom '+tablename +' UT ,Message M,#dep depwhere UT.userid='+convert(varchar(10),@i_USERID)+'and UT.messageid=M.idand M.deletedflag=0and UT.deletedflag=0'+@v_ishot+'and m.departmentid=dep.depidand m.mtid>='+CONVERT(VARCHAR(100),@v_beginmtid)+'and m.mtid<'+CONVERT(VARCHAR(100),@v_endmtid)+'and m.levelid>='+CONVERT(VARCHAR(100),@v_beginlevelid)+'and m.levelid<'+CONVERT(VARCHAR(100),@v_endlevelid)+'and m.modifydate>='+''''+@i_begintime+'''and m.modifydate<'+''''+@i_endtime+'''and m.title like ''%'+@i_title+'%'''+' ORDER BY M.ModifyDate DESC'FROM ADM_F_GETUSERREADTABLE (@i_USERID)--print @v_sqlstr--print @v_sqlstrEXEC (@v_sqlstr)--SELECT * FROM #countCREATE TABLE #showall(ids INT IDENTITY,messageID INT,--MTID INT,--Title VARCHAR(1000),isAttachment INT,DepartmentID INT,LevelID INT,IsRead INT,ModifyDate DATETIME,IsHot int)--print @v_sqlstrxEXEC (@v_sqlstrx)-- SELECT * FROM #showallIF @i_messageid IS NULLBEGINIF @i_isclient IS NOT NULLBEGINSELECT distinct ISNULL(@i_depid,0) AS rootdeptid,c.counts as msgcount,--a.ids,a.messageID as messageid,a.MTID as mtid,CASE WHEN m.needprove = '1' THEN '证' enda.Title as title,a.isAttachment as isattachment,a.DepartmentID as deptid,a.LevelID as levelid,a.IsRead as isread,a.ModifyDate as modifydate,a.IsHot as ishot,m.needproveFROM #showall a,#count c ,dbo.Message mWHERE m.ID = a.messageID AND a.ids>=(@i_currentpage-1)*@i_pagecount+1AND a.ids< (@i_currentpage+1)*@i_pagecountorder by a.ModifyDate descENDELSESELECT distinct ISNULL(@i_depid,0) AS rootdeptid,c.counts as msgcount,--a.ids,a.messageID as messageid,a.MTID as mtid,a.Title as title,a.isAttachment as isattachment,a.DepartmentID as deptid,a.LevelID as levelid,a.IsRead as isread,a.ModifyDate as modifydate,a.IsHot as ishot,m.needproveFROM #showall a,#count c ,dbo.Message mWHERE m.ID = a.messageID AND a.ids>=(@i_currentpage-1)*@i_pagecount+1AND a.ids< (@i_currentpage+1)*@i_pagecountorder by a.ModifyDate descENDENDELSEBEGINDECLARE @v_currentids INT,@v_prevmsgid INT,@v_nextmsgid INTselect @v_currentids=ids FROM #showall WHERE messageID=@i_messageidSELECT @v_prevmsgid=messageid FROM #showall WHERE ids=@v_currentids-1SELECT @v_nextmsgid=messageid FROM #showall WHERE ids=@v_currentids+1SELECT distinct M.*,@v_prevmsgid AS prevmsgid,@v_nextmsgid AS nextmsgid,shall.IsRead as isread INTO #shwFROM MESSAGE M,#showall shallWHERE M.id=@i_messageidAND m.ID=shall.messageIDIF EXISTS( SELECT 1 FROM #shw WHERE isread=0)BEGINexec [P_Message_User_Update_for_view] @i_userid,@i_messageidUPDATE [Message]SET Hits =ISNULL(hits,0)+1WHERE id=@i_messageidENDSELECT * FROM #shwENDENDGO