将表名变量发送到存储过程

时间:2017-03-07 19:05:15

标签: sql tsql sql-server-2014

我尝试这样做,但我不能这样做,请帮助:

将表的名称作为变量发送到存储过程,以对该变量进行选择查询。例如:

USE [Andes]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[epesos_ing_log]*/
@Tbl_OC_Llamada VARCHAR(128),-- TABLE's NAME
@Tbl_Est_Llamada VARCHAR(128) ,-- TABLE's NAME
@tbl_Resul_Llamada VARCHAR(128) -- TABLE's NAME

as

select  iId, 
        datepart(year, dFechaHora) as año_periodo, 
        datepart(MM, dFechaHora) as mes_periodo, 
        cast(dFechaHora as date) as dFecha, 
        datepart(HH, dFechaHora) as T_hora, 
        dFechaHora, dFin, 
        datediff (second, dFechaHora, dFin) as tiempo,
        sNumeroTelefono, 
        'Emisión Ext' as tipo, 
        (iAgenteEmisor) as Agente

from @Tbl_OC_Llamada 
    inner join @Tbl_Est_Llamada 
    on ELL.id_estado = T.iUltimoEstado
    inner join @tbl_Resul_Llamada 
    on RLL.id_res_llamada = T.iResultadoLlamada   

where sDireccion = 's' 
    and len (sNumeroTelefono) > 4
    and desc_estado in ('Hablando','Iniciada')
    and iAgenteEmisor in (select cod_ejecutiva
     from andes.dbo.BORRADOR_EJECUTIVA)

1 个答案:

答案 0 :(得分:0)

这是一个可以通过使用动态SQL解决的问题。 请查看此问题的答案:Table name as variable