将外部参数添加到存储过程

时间:2017-01-30 10:56:47

标签: sql-server

此存储过程执行正常:

insert into TEMP (name,surname) 
    select name, surname 
    from MEMBERS 
    where active = 1

但是,我想在程序中再添加两个参数(月,年)。

我试过了:

     @p1 date, 
     @p2 date 
AS
BEGIN
    INSERT INTO TEMP (name, surname, month, year) 
        SELECT 
            name, surname 
        FROM
            MEMBERS 
        WHERE
            active = 1, @p1, @p2
END

但它不起作用。为什么呢?

稍后我在我的程序中提供的参数如下:

sp.Params.ParamByName('p1').asdateTime :=formatdatetime('mm', dxDateTimeWheelPicker1.DateTime);
sp.Params.ParamByName('p2').asdateTime :=formatdatetime('yyyy',dxDateTimeWheelPicker1.DateTime);

2 个答案:

答案 0 :(得分:2)

实际上:

@p1 date, 
@p2 date 
AS
BEGIN
insert into TEMP (name,surname,month,year) 
select name,surname,@p1,@p2 from MEMBERS where active=1

参数已设置为......

答案 1 :(得分:-1)

创建表临时添加列(月,年),这两列应该是两个列的int数据类型

因为我们不能直接在插入语句中插入@ p1,@ p2 和@ p1和@ p2数据类型是我们需要采取的变量的日期时间,用户没有提及 所以我假设一个变量的月份和年份来自另一个变量

declare  @p1 date, 
     @p2 date 

    AS
BEGIN

insert into TEMP (name,surname,month,year) 
select name,surname,month(@p1),year(@p2) from MEMBERS where active=1

END