How to get a value from a SQL Server T-SQL record

0

I have the following code

USE [database]
GO
/****** Object:  StoredProcedure [dbo].[spPacienteEnCola]    Script Date: 02/23/2017 08:31:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spPacienteEnCola]
    @tabla varchar(20) = '',
    @donde varchar(50) = '',
    @funcion int = 0

AS
begin
    declare @VConsulta varchar(max) = ''
    declare @msg varchar(max) = ''

    if @funcion = 0
        begin
            set @msg = N'No has enviado el valor de @funcion'
            print @msg
        end

    if @funcion = 1
        begin
            set @VConsulta = 'select * from ' + @tabla + ' where ' + @donde
            exec(@vConsulta)
        end


end

In function 1, which makes the selection, I need to obtain the value of the Time column and compare it with the current time, but I can not find the way to get the value of the record since the procedure sends me the complete datatable, or the complete record. Help.

Even putting select hora , it's fine, it returns the time, but not as a data, but as a datatable. What I want is to have that data and assign it to a variable with set @hora = Hora_de_registro_en_bd

    
asked by Anthony Medina 23.02.2017 в 18:31
source

1 answer

1

Something like that will help you?

ALTER PROCEDURE [dbo].[spPacienteEnCola] 
@tabla varchar(50) = '',
@donde varchar(50) = '',
@funcion int = 0
AS
begin
    create table #temp (hora varchar(30))
    declare @VConsulta varchar(max) = ''
    declare @msg varchar(max) = ''
    declare @hora VARCHAR(MAX)
    declare @sql varchar(100)
    select @sql = 'insert into #temp select top 1 
    CONVERT(VARCHAR(8),hora,108) from '+@tabla+' where '+@donde+''
    exec (@sql)
if @funcion = 0
    begin
        set @msg = N'No has enviado el valor de @funcion'
        print @msg
    end

if @funcion = 1
    begin
        set @hora = (select hora from #temp)
        set @VConsulta = 'select * from ' + @tabla + ' where ' + @donde
        select @hora
        drop table #temp
    end
end
    
answered by 23.02.2017 / 18:50
source