sp_executesql如何选择?

时间:2016-04-27 17:54:23

标签: sql-server database stored-procedures sql-server-2000 sp-executesql

请。我需要运行存储在临时表中的存储过程。我有两张桌子:

create table pruebaa
(
    cam1 integer primary key,
    cam2 varchar(20)
)

insert into pruebaa (cam1, cam2) values (1, 'valor1');
insert into pruebaa (cam1, cam2) values (2, 'valor2');
insert into pruebaa (cam1, cam2) values (3, 'valor3');
insert into pruebaa (cam1, cam2) values (4, 'valor4');
insert into pruebaa (cam1, cam2) values (5, 'valor5');
insert into pruebaa (cam1, cam2) values (6, 'valor6');

create table pruebab
(
    cam1 integer primary key,
    cam2 varchar(20)
)

我需要将数据表A保存到表B但是存储过程。我这样做了:

create procedure insertapruebab(@cam1_ex int, @cam2_ex varchar(20))
as
begin
    insert into pruebab(cam1, cam2) values (@cam1_ex, @cam2_ex);
end

create table #querysEjecutar(
    campo1 varchar(2000)
)

insert into #querysEjecutar(campo1) (select ' exec insertapruebab '+ CONVERT(varchar(20), cam1)  + ', ' + CONVERT(varchar(20), cam2) from pruebaa);
declare @campoquery nvarchar(2000);
set @campoquery = 'select campo1 from #querysEjecutar';
exec sp_executesql @campoquery;
select * from pruebab;

但是在行中" exec sp_executesql"结果是:

 exec insertapruebab 1, valor1
 exec insertapruebab 2, valor2
 exec insertapruebab 3, valor3
 exec insertapruebab 4, valor4
 exec insertapruebab 5, valor5
 exec insertapruebab 6, valor6

由于

1 个答案:

答案 0 :(得分:0)

您可以使用以下脚本替换动态SQL:

    Drawable bg;
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        bg = VectorDrawableCompat.create(a.getResources(), R.drawable.ic_exit_to_app_24dp, null);
        exitButton.setColorFilter(headerTitleColor, PorterDuff.Mode.MULTIPLY);
    }
    else {
        bg = ContextCompat.getDrawable(a, R.drawable.ic_exit_to_app_24dp);
        DrawableCompat.setTint(bg, headerTitleColor);
    }
    exitButton.setImageDrawable(bg);

这基本上会为pruebaa表中的每一行调用insertapruebab。