當前位置:首頁 » 編程語言 » csql返回

csql返回

發布時間: 2025-01-29 09:42:18

『壹』 c#怎麼和sql資料庫連接

1、打開Visual Studio 2008工具,點擊文件菜單,選擇新建下面的項目選項,如下圖所示。

『貳』 SQL Server 在一個存儲過程中調用另外一個存儲過程獲取返回值,出現報錯

第一種方法: 使用output參數

USE AdventureWorks;
GO
IF OBJECT_ID ( 'Proction.usp_GetList', 'P' ) IS NOT NULL
DROP PROCEDURE Proction.usp_GetList;
GO
CREATE PROCEDURE Proction.usp_GetList @proct varchar(40)
, @maxprice money
, @compareprice money OUTPUT
, @listprice money OUT
AS
SELECT p.name AS Proct, p.ListPrice AS 'List Price'
FROM Proction.Proct p
JOIN Proction.ProctSubcategory s
ON p.ProctSubcategoryID = s.ProctSubcategoryID
WHERE s.name LIKE @proct AND p.ListPrice < @maxprice;
-- Populate the output variable @listprice.
SET @listprice = (SELECT MAX(p.ListPrice)
FROM Proction.Proct p
JOIN Proction.ProctSubcategory s
ON p.ProctSubcategoryID = s.ProctSubcategoryID
WHERE s.name LIKE @proct AND p.ListPrice < @maxprice);
-- Populate the output variable @compareprice.
SET @compareprice = @maxprice;
GO

另一個存儲過程調用的時候:

Create Proc Test
as
DECLARE @compareprice money, @cost money
EXECUTE Proction.usp_GetList '%Bikes%', 700,
@compareprice OUT,
@cost OUTPUT
IF @cost <= @compareprice
BEGIN
PRINT 'These procts can be purchased for less than
$'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
END
ELSE
PRINT 'The prices for all procts in this category exceed
$'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
第二種方法:創建一個臨時表

create proc GetUserName
as
begin
select 'UserName'
end

Create table #tempTable (userName nvarchar(50))
insert into #tempTable(userName)
exec GetUserName

select #tempTable

--用完之後要把臨時表清空
drop table #tempTable--需要注意的是,這種方法不能嵌套。例如:

procere a
begin
...
insert #table exec b
end

procere b
begin
...
insert #table exec c
select * from #table
end

procere c
begin
...
select * from sometable
end

--這里a調b的結果集,而b中也有這樣的應用b調了c的結果集,這是不允許的,
--會報「INSERT EXEC 語句不能嵌套」錯誤。在實際應用中要避免這類應用的發生。

第三種方法:聲明一個變數,用exec(@sql)執行:

1);EXEC 執行SQL語句

declare @rsql varchar(250)
declare @csql varchar(300)
declare @rc nvarchar(500)
declare @cstucount int
declare @ccount int
set @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''否'') and ClassroomType=''1'''
--exec(@rsql)
set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in '
set @rc=@csql+@rsql
exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--將exec的結果放入變數中的做法
--select @csql+@rsql
--select @cstucount

熱點內容
app什麼情況下找不到伺服器 發布:2025-05-12 15:46:25 瀏覽:714
php跳過if 發布:2025-05-12 15:34:29 瀏覽:467
不定時演算法 發布:2025-05-12 15:30:16 瀏覽:131
c語言延時1ms程序 發布:2025-05-12 15:01:30 瀏覽:166
動物園靈長類動物配置什麼植物 發布:2025-05-12 14:49:59 瀏覽:736
wifi密碼設置什麼好 發布:2025-05-12 14:49:17 瀏覽:148
三位數乘兩位數速演算法 發布:2025-05-12 13:05:48 瀏覽:398
暴風影音緩存在哪裡 發布:2025-05-12 12:42:03 瀏覽:544
access資料庫exe 發布:2025-05-12 12:39:04 瀏覽:632
五開的配置是什麼 發布:2025-05-12 12:36:37 瀏覽:365