读书人

这四句代码看不明白请老师们讲讲多

发布时间: 2012-02-03 22:02:47 作者: rapoo

这四句代码看不明白,请老师们讲讲,谢谢啊!
declare @num int,@sqls nvarchar(4000)
set @sqls= 'select @a=count(*) from gzda '
exec sp_executesql @sqls,N '@a int output ',@num output
select @num

我不明白的是 @a参数,@num参数的这种写法是什么意思呀,帮助也看了,不明白。

[解决办法]

Transact-SQL 程式法的考明


sp_executesql
行可以重新使用多次或已建立的 Transact-SQL 述式或批次。Transact-SQL 述式或批次可以包含嵌。


sp_executesql [@stmt =] stmt
[
{, [@params =] N '@parameter_name data_type [,...n] ' }
{, [@param1 =] 'value1 ' [,...n] }
]


[@stmt =] stmt

包含 Transact-SQL 述式或批次的 Unicode 字串。stmt m 必是 Unicode 常或可以以含方式 ntext 的。更的 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 的所有之定的字串。字串必是 Unicode 常或可以以含方式 ntext 的。每定包含一名料型。n 是指出外定的替代符 (Placeholder)。stmt 中指定的每都必在 @params中定。如果 stmt 中的 Transact-SQL 述式或批次不包含,就不需要 @params。的值 NULL。

[@param1 =] 'value1 '

字串中定的第一值。值可以是常或。stmt 中包括的每都必有值。如果 stmt 中的 Transact-SQL 述式或批次有,就不需要值。

n

外值的替代符。值可以只是常或。值不可以是函或使用算子建立的算式等的算式。

回值
0 (成功) 或 1 (失)

果集
建在 SQL 字串的所有 SQL 述式回果集。


在批次、名料容方面,sp_executesql EXECUTE 的行相同。直到行 sp_executesql 述式之後,系才 sp_executesql stmt 中的 Transact-SQL 述式批次。系接著行 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 字串。


在其原生 (Native) 格式中指定整。不需要 Unicode。

行限授予 public 角色。


A. 行的 SELECT 述式
以下例建立行一的 SELECT 述式,其中包含命名 @level 的嵌。

execute sp_executesql
N 'select * from pubs.dbo.employee where job_lvl = @level ',
N '@level tinyint ',
@level = 35

B. 行建立的字串
以下例示使用 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, @PrmDeliveryDate

GO

在此程序中使用 sp_executesql 比使用 EXECUTE 行字串更有效率。使用 sp_executesql ,只生 12 版本的 INSERT 字串,每月份料表 1 。使用 EXECUTE ,每 INSERT 字串都是唯一的,因值不同。然二方法都生相同目的批次,但是 sp_executesql 生的INSERT 字串之相似性可能使查最佳化器重覆使用行。






批次

EXECUTE

於行期建立述式

系存程序

©1988-2000 Microsoft Corporation. All Rights Reserved.

[解决办法]
@a int output
表示此参数可以返回值

@a和@num必须成对出现,@num为返回变量
[解决办法]
declare @user varchar(1000)declare @moTable varchar(20)select @moTable = 'MT_10 'declare @sql nvarchar(4000) --定义变量,注意类型set @sql= 'select @user = count(distinct userid) from '+@moTable --为变量赋值--执行@sql中的语句exec sp_executesql @sql ,N '@user varchar(1000) out ' --表示@sql中的语句包含了一个输出参数 ,@user out --和调用存储过程差不多,指定输出参数值print @user

读书人网 >SQL Server

热点推荐