Parameter in a Query

  • Hi all

    I have a sp:

    alter procedure spTotalCompraIdiomaLibro @Idioma varchar(20), @año varchar(10)

    as

    select Count(*) as TotalLibrosAdquiridos

    from dbo.Ejemplares E, LibrosIdiomas LI, Idiomas I, Libros L

    where E.IDLibro = LI.IDLibro

    and E.IDLibro = L.IDLibro

    and LI.IDIdioma = I.IDIdioma

    and E.IDTipoAdquisicion = 1

    and I.Idioma = @Idioma

    and E.FechaRecepcion like '%' + @año

    How can i put a parameter in a title?

    for example

    select Count(*) as TotalLibrosAdquiridos+@Año

    but it doesnt work 🙁

    thanks any help ^^

  • You can use dynamic sql like:

    ALTER PROCEDURE spTotalCompraIdiomaLibro @Idioma varchar(20), @año varchar(10)

    AS

    BEGIN

    DECLARE @Some_Sql VARCHAR(8000)

    SET @Some_Sql =

    'select Count(*) as TotalLibrosAdquiridos' + @año +

    ' from dbo.Ejemplares E, LibrosIdiomas LI, Idiomas I, Libros L

    where E.IDLibro = LI.IDLibro

    and E.IDLibro = L.IDLibro

    and LI.IDIdioma = I.IDIdioma

    and E.IDTipoAdquisicion = 1

    and I.Idioma = @Idioma

    and E.FechaRecepcion like ''%' + @año + ''''

    EXEC(@Some_Sql)

    END

  • Hi, thank for your help ^^

    alter PROCEDURE spTotalCompraIdiomaLibroDina @Idioma varchar(20), @año varchar(10)

    AS

    BEGIN

    DECLARE @Some_Sql VARCHAR(8000)

    SET @Some_Sql =

    'select Count(*) as TotalLibrosAdquiridos' + @año +

    ' from dbo.Ejemplares E, LibrosIdiomas LI, Idiomas I, Libros L

    where E.IDLibro = LI.IDLibro

    and E.IDLibro = L.IDLibro

    and LI.IDIdioma = I.IDIdioma

    and E.IDTipoAdquisicion = 1

    and I.Idioma = '+@Idioma+

    ' and E.FechaRecepcion like ''%' + @año + ''''

    EXEC(@Some_Sql)

    END

    I execute like this:

    exec spTotalCompraIdiomaLibroDina 'Español','1995'

    but it throws an error: The name of the column 'Español' is not valid.

    i work in spanish: El nombre de columna 'Español' no es válido.

    thank you

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply