SqlDataAdapter da = new SqlDataAdapter("select * from aa where a='1'",myConn); DataSet ds = new DataSet(); da.Fill(ds,"temp"); DataTable dt = new DataTable(); dt = ds.Tables["temp"]; string strValue = dt.Rows[0][0].ToString(); 不知道以上这些能否实现你想要的功能.
你想在一个存储过程里返回值?然后在另一个存储过程里取得? 下面是一个有返回值的例子 USE pubs GO CREATE PROCEDURE myProc @outparm int OUTPUT @inparm int AS SELECT * FROM titles WHERE royalty > @inparm SELECT @outparm = COUNT (*) FROM TITLES WHERE royalty > @inparm IF (@outparm > 0) RETURN 0 ELSE RETURN 99 GO在其他地方调用 declare @back int exec myProc '...',@back output
USE prj IF EXISTS (SELECT name FROM sysobjects WHERE name = 'pp_update' AND type ='P') DROP PROCEDURE pp_update GO USE prj GO CREATE PROCEDURE pp_update @day bigint, --當天的生產量 @componentID int, @projectID int, @processID int AS--set @days 'day'+cast(day(getdate()) as char(2)) begin declare @days char(5) --獲得當天的字段名 set @days=rtrim ('day' + LTRIM(str (day(getdate()) ) )) --print @days --得到更新的SQL字符串 declare @statement NVARCHAR(500) select @statement = 'update pt_prjmng set ' select @statement = @statement + @days select @statement = @statement + '='+convert(varchar,@day)+' ' select @statement = @statement+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''0''' --print @statement --執行SQL字符串 EXEC (@STATEMENT) end --更新累積產量 declare @dayss char(5) set @dayss=rtrim ('day' + LTRIM(str (day(getdate())-1 ) )) if day(getdate())=1 --如果是月初,累積量=期初+當天產量 begin
declare @accumulation1 bigint declare @statement1 NVARCHAR(500) select @statement1='set @accumulation1=select InitialNumber from pt_prjmng ' select @statement1 = @statement1+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1''' select @statement1 =@statement+ ' update pt_prjmng set ' select @statement1 = @statement1 + @days select @statement1 = @statement1 + '='+convert(varchar,@accumulation1+@day)+' ' select @statement1 = @statement1+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1''' --print @statement EXEC (@STATEMENT1) end else --否則,累積量=前一天的累積量+當天產量 begin declare @accumulation2 bigint declare @statement2 NVARCHAR(500) select @statement2='select '+@dayss+'from pt_prjmng ' select @statement2 = @statement2+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1''' --select @statement2=' set '+convert(varchar,@accumulation2) +'= isnull((select '+@dayss+' from pt_prjmng ' --select @statement2= @statement2+' where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1''' -- select @statement2=@statement2+',0) select '+convert(varchar,@accumulation2) -- exec(@statement2) print @statement2 -- declare @statement2 NVARCHAR(500) --set @statement2=(select day4 from pt_prjmng where componentID='1' and projectID='2' and processID='1' and judge='1') exec (@statement2) --print @accumulation2 --declare @statement3 nvarchar(500) --select @statement3 = ' update pt_prjmng set ' -- select @statement3= @statement3 + @days -- select @statement3= @statement3+ '='+convert(varchar,@accumulation2+@day)+' ' -- select @statement3= @statement3+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1''' -- print @statement3 -- EXEC (@STATEMENT2) endGOexec pp_update 400,1,2,1
DataSet ds = new DataSet();
da.Fill(ds,"temp");
DataTable dt = new DataTable();
dt = ds.Tables["temp"];
string strValue = dt.Rows[0][0].ToString();
不知道以上这些能否实现你想要的功能.
下面帖子的问题其实还没解决
我一不小心结了帖子
http://expert.csdn.net/Expert/topic/2921/2921468.xml?temp=.6456415
下面是一个有返回值的例子
USE pubs
GO
CREATE PROCEDURE myProc
@outparm int OUTPUT
@inparm int
AS
SELECT * FROM titles WHERE royalty > @inparm
SELECT @outparm = COUNT (*) FROM TITLES WHERE royalty > @inparm
IF (@outparm > 0)
RETURN 0
ELSE
RETURN 99
GO在其他地方调用
declare @back int
exec myProc '...',@back output
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'pp_update' AND type ='P')
DROP PROCEDURE pp_update
GO
USE prj
GO
CREATE PROCEDURE pp_update
@day bigint, --當天的生產量
@componentID int,
@projectID int,
@processID int
AS--set @days 'day'+cast(day(getdate()) as char(2))
begin
declare @days char(5)
--獲得當天的字段名
set @days=rtrim ('day' + LTRIM(str (day(getdate()) ) ))
--print @days
--得到更新的SQL字符串
declare @statement NVARCHAR(500)
select @statement = 'update pt_prjmng set '
select @statement = @statement + @days
select @statement = @statement + '='+convert(varchar,@day)+' '
select @statement = @statement+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''0'''
--print @statement
--執行SQL字符串
EXEC (@STATEMENT)
end
--更新累積產量
declare @dayss char(5)
set @dayss=rtrim ('day' + LTRIM(str (day(getdate())-1 ) ))
if day(getdate())=1
--如果是月初,累積量=期初+當天產量
begin
declare @accumulation1 bigint
declare @statement1 NVARCHAR(500)
select @statement1='set @accumulation1=select InitialNumber from pt_prjmng '
select @statement1 = @statement1+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1'''
select @statement1 =@statement+ ' update pt_prjmng set '
select @statement1 = @statement1 + @days
select @statement1 = @statement1 + '='+convert(varchar,@accumulation1+@day)+' '
select @statement1 = @statement1+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1'''
--print @statement
EXEC (@STATEMENT1)
end
else
--否則,累積量=前一天的累積量+當天產量
begin
declare @accumulation2 bigint
declare @statement2 NVARCHAR(500)
select @statement2='select '+@dayss+'from pt_prjmng '
select @statement2 = @statement2+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1'''
--select @statement2=' set '+convert(varchar,@accumulation2) +'= isnull((select '+@dayss+' from pt_prjmng '
--select @statement2= @statement2+' where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1'''
-- select @statement2=@statement2+',0) select '+convert(varchar,@accumulation2)
-- exec(@statement2)
print @statement2
-- declare @statement2 NVARCHAR(500)
--set @statement2=(select day4 from pt_prjmng where componentID='1' and projectID='2' and processID='1' and judge='1')
exec (@statement2)
--print @accumulation2
--declare @statement3 nvarchar(500)
--select @statement3 = ' update pt_prjmng set '
-- select @statement3= @statement3 + @days
-- select @statement3= @statement3+ '='+convert(varchar,@accumulation2+@day)+' '
-- select @statement3= @statement3+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1'''
-- print @statement3
-- EXEC (@STATEMENT2)
endGOexec pp_update 400,1,2,1
我是想把
exec(@statement2的值
传给变量@accumulation
1:如果你是要得到一个变量的值 @accumulation ,你可以 设置这个变量为 output;
eg:
CREATE PROCEDURE pp_update
@day bigint, --當天的生產量
@componentID int,
@projectID int,
@processID int
@accumulation bigint output
AS
在代码中调用 c#
SqlCommand cmd = new SqlCommand();
cmd.connection = con;
cmd.CommandText = 'exec pp_update 400,1,2,1 ,@acum output'
cmd.Parameters.Add("@acum" , 0);
cmd.Parameters["@acum"].Direction = ParameterDirection.InputOutput;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Parameters["@acum"].Value; ----你需要的值;付给一个变量即可;
要把语句内的参数和语句外的参数分开
SET @statement= 'SELECT @accumulation ........'
exec sp_executesql @statement,N'@accumulation bigint output',@accumulation output
.......
这样可以把@statement 里的@accumulation变量值赋给语句外的@accumulation
执行可以多次重用或动态生成的 Transact-SQL 语句或批处理。Transact-SQL 语句或批处理可以包含嵌入参数。语法
sp_executesql [@stmt =] stmt
[
{, [@params =] N'@parameter_name data_type [,...n]' }
{, [@param1 =] 'value1' [,...n] }
]参数
[@stmt =] stmt包含 Transact-SQL 语句或批处理的 Unicode 字符串,stmt 必须是可以隐式转换为 ntext 的 Unicode 常量或变量。不允许使用更复杂的 Unicode 表达式(例如使用 + 运算符串联两个字符串)。不允许使用字符常量。如果指定常量,则必须使用 N 作为前缀。例如,Unicode 常量 N'sp_who' 是有效的,但是字符常量 'sp_who' 则无效。字符串的大小仅受可用数据库服务器内存限制。stmt 可以包含与变量名形式相同的参数,例如:N'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'stmt 中包含的每个参数在 @params 参数定义列表和参数值列表中均必须有对应项。[@params =] N'@parameter_name data_type [,...n]'字符串,其中包含已嵌入到 stmt 中的所有参数的定义。该字符串必须是可以隐式转换为 ntext 的 Unicode 常量或变量。每个参数定义均由参数名和数据类型组成。n 是表明附加参数定义的占位符。stmt 中指定的每个参数都必须在 @params 中定义。如果 stmt 中的 Transact-SQL 语句或批处理不包含参数,则不需要 @params。该参数的默认值为 NULL。[@param1 =] 'value1'参数字符串中定义的第一个参数的值。该值可以是常量或变量。必须为 stmt 中包含的每个参数提供参数值。如果 stmt 中包含的 Transact-SQL 语句或批处理没有参数,则不需要值。n附加参数的值的占位符。这些值只能是常量或变量,而不能是更复杂的表达式,例如函数或使用运算符生成的表达式。返回代码值
0(成功)或 1(失败)结果集
从生成 SQL 字符串的所有 SQL 语句返回结果集。注释
在批处理、名称作用域和数据库上下文方面,sp_executesql 与 EXECUTE 的行为相同。sp_executesql stmt 参数中的 Transact-SQL 语句或批处理在执行 sp_executesql 语句时才编译。然后编译 stmt 中的内容并作为执行计划运行(独立于名为 sp_executesql 的批处理的执行计划)。sp_executesql 批处理不能引用调用 sp_executesql 的批处理中声明的变量。sp_executesql 批处理中的本地游标和变量对调用 sp_executesql 的批处理是不可见的。对数据库上下文所作的更改只在 sp_executesql 语句结束前有效。如果只更改了语句中的参数值,则 sp_executesql 可用来代替存储过程多次执行 Transact-SQL 语句。因为 Transact-SQL 语句本身保持不变仅参数值变化,所以 Microsoft® SQL Server™ 查询优化器可能重复使用首次执行时所生成的执行计划。说明 如果语句字符串中的对象名不是全限定名,则该执行计划不会被重用。
sp_executesql 支持与 Transact-SQL 字符串相独立的参数值的设置:DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)/* Build the SQL string once.*/
SET @SQLString =
N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'
SET @ParmDefinition = N'@level tinyint'
/* Execute the string with the first parameter value. */
SET @IntVariable = 35
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable
/* Execute the same string with the second parameter value. */
SET @IntVariable = 32
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable替换 sp_executesql 中的参数的能力,与使用 EXECUTE 语句执行字符串相比,有下列优点: 因为在 sp_executesql 中,Transact-SQL 语句的实际文本在两次执行之间未改变,所以查询优化器应该能将第二次执行中的 Transact-SQL 语句与第一次执行时生成的执行计划匹配。这样,SQL Server 不必编译第二条语句。
Transact-SQL 字符串只生成一次。
整型参数按其本身格式指定。不需要转换为 Unicode。
权限
执行权限默认授予 public 角色。示例
A. 执行简单的 SELECT 语句
下面的示例创建并执行一个简单的 SELECT 语句,其中包含名为 @level 的嵌入参数。execute sp_executesql
N'select * from pubs.dbo.employee where job_lvl = @level',
N'@level tinyint',
@level = 35B. 执行动态生成的字符串
下面的示例显示使用 sp_executesql 执行动态生成的字符串。该示例中的存储过程用来向一组表中插入数据,该表用于划分一年的销售数据。一年中的每个月均有一个表,格式如下:CREATE TABLE May1998Sales
(OrderID INT PRIMARY KEY,
CustomerID INT NOT NULL,
OrderDate DATETIME NULL
CHECK (DATEPART(yy, OrderDate) = 1998),
OrderMonth INT
CHECK (OrderMonth = 5),
DeliveryDate DATETIME NULL,
CHECK (DATEPART(mm, OrderDate) = OrderMonth)
)有关从这些分区表中检索数据的更多信息,请参见使用包含分区数据的视图。 每个表的名称由月份名的前三个字母、年度的四位数字和常量 Sales 组成。名称可以从订单日期动态生成:/* Get the first three characters of the month name. */
SUBSTRING( DATENAME(mm, @PrmOrderDate), 1, 3) +
/* Concatenate the four-digit year; cast as character. */
CAST(DATEPART(yy, @PrmOrderDate) AS CHAR(4) ) +
/* Concatenate the constant 'Sales'. */
'Sales'下面示例中的存储过程动态生成并执行一个 INSERT 语句,向适当的表中插入新订单。该存储过程使用订单日期生成应包含数据的表的名称,然后将名称并入 INSERT 语句。(这是 sp_executesql 的一个简单示例。不包含错误检查,也不包括业务规则检查,例如确保两个表之间订单号没有重复。)CREATE PROCEDURE InsertSales @PrmOrderID INT, @PrmCustomerID INT,
@PrmOrderDate DATETIME, @PrmDeliveryDate DATETIME
AS
DECLARE @InsertString NVARCHAR(500)
DECLARE @OrderMonth INT-- Build the INSERT statement.
SET @InsertString = 'INSERT INTO ' +
/* Build the name of the table. */
SUBSTRING( DATENAME(mm, @PrmOrderDate), 1, 3) +
CAST(DATEPART(yy, @PrmOrderDate) AS CHAR(4) ) +
'Sales' +
/* Build a VALUES clause. */
' VALUES (@InsOrderID, @InsCustID, @InsOrdDate,' +
' @InsOrdMonth, @InsDelDate)'/* Set the value to use for the order month because
functions are not allowed in the sp_executesql parameter
list. */
SET @OrderMonth = DATEPART(mm, @PrmOrderDate)EXEC sp_executesql @InsertString,
N'@InsOrderID INT, @InsCustID INT, @InsOrdDate DATETIME,
@InsOrdMonth INT, @InsDelDate DATETIME',
@PrmOrderID, @PrmCustomerID, @PrmOrderDate,
@OrderMonth, @PrmDeliveryDateGO在该过程中使用 sp_executesql 比使用 EXECUTE 执行字符串更有效。使用 sp_executesql 时,只生成 12 个版本的 INSERT 字符串,每个月的表 1 个。使用 EXECUTE 时,因为参数值不同,每个 INSERT 字符串均是唯一的。尽管两种方法生成的批处理数相同,但因为 sp_executesql 生成的 INSERT 字符串相似,所以查询优化程序更有可能反复使用执行计划。
感谢setu1(se) 的帮助.
现在已经可以正确得出结果了
代码如下:
declare @accumulation2 bigint
declare @statement2 NVARCHAR(500) select @statement2=' set @accumulation2= (select '+@dayss+' from pt_prjmng '
select @statement2= @statement2+' where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1'')'--print @statement2 exec sp_executesql @statement2,N'@accumulation2 bigint output',@accumulation2 output
-- print @accumulation2
declare @statement3 nvarchar(500)
select @statement3 = ' update pt_prjmng set '
select @statement3= @statement3 + @days
select @statement3= @statement3+ '='+convert(varchar,@accumulation2+@day)+' '
select @statement3= @statement3+'where componentID='''+convert(varchar,@componentID)+''' and projectID='''+convert(varchar,@projectID)+''' and processID='''+convert(varchar,@processID)+''' and judge=''1'''
-- print @statement3
EXEC (@STATEMENT3)