1 --sql code Geovin Du 2 IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_AssessmentProjectSetSearch') 3 DROP PROCEDURE proc_Select_AssessmentProjectSetSearch 4 GO 5 CREATE PROCEDURE proc_Select_AssessmentProjectSetSearch 6 ( 7 @AssessmentProjectSetJobType Uniqueidentifier, --考核職位類型 8 @AssessmentProjectSetUidKey nvarchar(2000) output --考核項目 9 ) 10 AS 11 declare @sql nvarchar(4000),@where nvarchar(4000)--,@AssessmentProjectSetUidKey nvarchar(4000) 12 set @where='' 13 select @AssessmentProjectSetUidKey=cast(AssessmentProjectSetUidKey as nvarchar(4000)) from AssessmentProjectSet where AssessmentProjectSetJobType=@AssessmentProjectSetJobType 14 set @sql='SELECT * FROM AssessmentProjectList where AssessmentProjectID in(' 15 if(@AssessmentProjectSetUidKey IS null or @AssessmentProjectSetUidKey='') 16 begin 17 set @AssessmentProjectSetUidKey='1' 18 SET @where=@AssessmentProjectSetUidKey 19 end 20 else 21 begin 22 set @where=@AssessmentProjectSetUidKey 23 end 24 --print @AssessmentProjectSetUidKey 25 set @sql=@sql+@where+')' 26 --print @sql 27 exec (@sql) 28 GO 29 30 --測試 31 declare @s nvarchar(2220) 32 exec proc_Select_AssessmentProjectSetSearch '7606598a-34da-43e3-a107-950ae724b80d',@s output 33 select @s 34 35 --- 36 IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_AssessmentProjectSetReturnSearch') 37 DROP PROCEDURE proc_Select_AssessmentProjectSetReturnSearch 38 GO 39 CREATE PROCEDURE proc_Select_AssessmentProjectSetReturnSearch 40 ( 41 @AssessmentProjectSetJobType Uniqueidentifier, --考核職位類型 42 @AssessmentProjectSetUidKey nvarchar(2000) output --考核項目 43 ) 44 AS 45 declare @sql nvarchar(4000),@where nvarchar(4000),@return int --,@AssessmentProjectSetUidKey nvarchar(4000) 46 set @where='' 47 select @AssessmentProjectSetUidKey=cast(AssessmentProjectSetUidKey as nvarchar(4000)) from AssessmentProjectSet where AssessmentProjectSetJobType=@AssessmentProjectSetJobType 48 set @sql='SELECT * FROM AssessmentProjectList where AssessmentProjectID in('+@AssessmentProjectSetUidKey+')' 49 50 exec (@sql) 51 if(@@error>0) 52 set @return=2 53 else 54 set @return=1 55 return @return 56 --return @AssessmentProjectSetUidKey --C# 中操作不可以字符類型 SQL上可以查詢 57 58 GO 59 60 --測試 61 declare @s nvarchar(2220) 62 exec proc_Select_AssessmentProjectSetReturnSearch '330FEE4C-A17E-46A4-9460-CA4DEB782ADF',@s output 63 select @s
csharp code:
1 /// <summary> 2 /// 塗聚文 20130119 3 /// 返回輸出參數 4 /// </summary> 5 /// <param name="assessmentProjectSetJobType"></param> 6 /// <param name="Return">輸出參數</param> 7 /// <returns></returns> 8 public DataTable SelectAssessmentProjectSetJobTypeDataTableSearch(Guid assessmentProjectSetJobType,out string Return) 9 { 10 DataTable dt = new DataTable(); 11 try 12 { 13 // SqlParameter par = new SqlParameter("@AssessmentProjectSetJobType ", assessmentProjectSetJobType); 14 SqlParameter[] par = new SqlParameter[]{ 15 //new SqlParameter("@AssessmentProjectSetID",SqlDbType.Int,4), 16 new SqlParameter("@AssessmentProjectSetJobType",SqlDbType.UniqueIdentifier), 17 new SqlParameter("@AssessmentProjectSetUidKey",SqlDbType.NVarChar,4000), 18 //new SqlParameter("@return",SqlDbType.Int,8), 19 }; 20 par[0].Value = assessmentProjectSetJobType; 21 par[1].Direction = ParameterDirection.Output; 22 23 using (DataTable reader = DBHelper.GetTable("proc_Select_AssessmentProjectSetSearch", CommandType.StoredProcedure, par)) 24 { 25 dt = reader; 26 Return =(string)par[1].Value.ToString(); 27 } 28 } 29 catch (SqlException ex) 30 { 31 32 throw ex; 33 } 34 return dt; 35 } 36 /// <summary> 37 /// 塗聚文 20130119 38 /// 返回自定義參數 39 /// </summary> 40 /// <param name="assessmentProjectSetJobType"></param> 41 /// <param name="Return">自定義參數</param> 42 /// <returns></returns> 43 public DataTable SelectAssessmentProjectSetReturnDataTableSearch(Guid assessmentProjectSetJobType, out string Return) 44 { 45 DataTable dt = new DataTable(); 46 try 47 { 48 // SqlParameter par = new SqlParameter("@AssessmentProjectSetJobType ", assessmentProjectSetJobType); 49 SqlParameter[] par = new SqlParameter[]{ 50 //new SqlParameter("@AssessmentProjectSetID",SqlDbType.Int,4), 51 new SqlParameter("@AssessmentProjectSetJobType",SqlDbType.UniqueIdentifier), 52 new SqlParameter("@AssessmentProjectSetUidKey",SqlDbType.NVarChar,4000), 53 new SqlParameter("@return",SqlDbType.Int,8), 54 }; 55 par[0].Value = assessmentProjectSetJobType; 56 par[1].Direction = ParameterDirection.Output; 57 par[2].Direction = ParameterDirection.ReturnValue;//ReturnValue返回值數值類型 58 using (DataTable reader = DBHelper.GetTable("proc_Select_AssessmentProjectSetReturnSearch", CommandType.StoredProcedure, par)) 59 { 60 dt = reader; 61 Return = (string)par[1].Value.ToString(); 62 int f = (int)par[2].Value; 63 } 64 } 65 catch (SqlException ex) 66 { 67 68 throw ex; 69 } 70 return dt; 71 }