DECLARE @F001 SMALLINT, (三元素,声明declare+变量名+类型)
@F002 INTEGER,
@F003 VARCHAR(20),
@F004 CHAR(20),
@@F002 MONEY
IF condition BEGIN
[statements ]
END
ELSE BEGIN (ELSE IF condition2 BEGIN)
[elseifstatements ]
END
SET @F011 =
CASE
WHEN [testexpression1] THEN @F001
WHEN [testexpression2] THEN @F002
WHEN [testexpression3] THEN @F003
WHEN [testexpression4] THEN @F004
END
WHILE condition BEGIN
[statements ]
END
DECLARE @strSQL nvarchar(30)
DECLARE @inSQL nvarchar(50) -- select…
SET @strSQL = ' DECLARE name_cursor CURSOR FOR
' + @inSQL
EXEC (@strSQL)
FETCH NEXT FROM name_cursor into@F001,@F002 (要求与游标的返回值对应)
WHILE @@FETCH_STATUS = 0 BEGIN (如@inSQL是selectf001,f002 from tablename)
FETCH NEXT FROM name_cursor into @F001,@F002
END (说明:FETCH_STATUS检索到数据返回0,失败返回-1,可判断是否滚动未到结尾。)
SET @RECCNT = @@ROWCOUNT --内嵌参数@@FETCH_STATUS
同
BEGIN distributed transaction
WHILE @@TRANCOUNT > 0
commit transaction
SET @m_sql = @m_sql + ' Where F001 = ''' +@F001 + ''''
SET @m_sql = @m_sql + ' F002 = ' + CONVERT(varchar,@F002) convert会加引号?
CREATE PROCEDURE material_solo_days_proc
(@materialnovarchar(20),
@year_month datetime,
@totalday int,
@qty decimal(18,4)=0 output
--总量,也会return
)
AS
BEGIN
DECLARE@@count
int
SET@@count = 0
DECLARE@@dailyqty
decimal(18,4)
SET@@dailyqty
= 0
SET@qty = 0
WHILE@@count < @totalday
BEGIN
--计算日流水量
EXECUTE @@dailyqty
= material_solo_oneday_proc @materialno,@year_month
SET @year_month
= dateadd(dd,1,@year_month)
SET @@count
= @@count + 1
END
RETURN@qty
END
withmaterial_account_proc do
begin
Close;
ProcedureName:='material_account_proc';
Parameters.Clear;
Parameters.Refresh;
Parameters.ParamByName('@year_month').Value:= year_month;
Parameters.ParamByName('@condition').Value:= ''; //string不匹配nvarchar
open;
y :=Parameters.ParamByName('@count').Value;
x := Parameters.ParamByName('@totalday').Value;
end;
DECLARE @C001 VARCHAR(20),
@C002 SMALLINT
EXEC name_produce @C001,@C002 output
/////////////////////////////////////////////////////////////////////////////////
CREATE PROCEDURE usp_GetSalesHistory
(
@WhereClauseNVARCHAR(2000)=NULL
)
AS
BEGIN
DECLARE @SelectStatement NVARCHAR(2000)
DECLARE @FullStatement NVARCHAR(4000)
SET @SelectStatement = 'SELECT * FROM SalesHistory'
SET @FullStatement = @SelectStatement +ISNULL(@WhereClause,' ')
PRINT @FullStatement
EXECUTE sp_executesql @FullStatement
/*EXECUTE(@FullStatement) */
END
说明: sp_executesql
执行的不能是varchar只能是nvarchar,sp_executesql
支持独立于 Transact-SQL
字符串设置参数值,有更多例子网上搜。
Execute可以是varchar,而且execute的sql语句中的参数只能是属性列或者表
--√
Update name_table set
F001 = @F181,
F002 = @F182
Where
F003 = @F003
--×
Update name_table
F001 = @F181,
F002 = @F182
Where
F003 = @F003
--×
Update name_table set
F001 = @F181,
F002 = @F182,
Where
F003 = @F003
--×
Update name_table set
,F001 = @F181
,F002 = @F182
Where
F003 = @F003
--√
INSERT INTO name_table(
,KEY_FIELD,BUSYOCD
)Values(
@F001,@F002
)
--×
INSERT INTO name_table(
F001,F002
)Values(
,@F001,@F002
)
--×
INSERT INTO name_table(
F001,F002
)Values(
@F001,@F002,
)
--×
INSERT name_table(
F001,F002
)Values(
@F001,@F002
)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章