Must declare the scalar variable "@IDAlbum"

Résolu
Hadil6 Messages postés 8 Date d'inscription   Statut Membre Dernière intervention   -  
Hadil6 Messages postés 8 Date d'inscription   Statut Membre Dernière intervention   -
Salut,

Je suis entrain de creer un simple website sous ASP.net et SQL server.

A l'execution, j'ai tjr le message (Must declare the scalar variable "@IDAlbum") lors de l'appel de la procedure Pager (vous pourriez voir le code ci-dessous). J'ai bien cherché sur le net et je ne trouve pas la solution appropriée...

Auriez vous une solution à me proposer ?

-----------------------------------------------
 USE [Portfolio]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Pager]
    (@PageNo int = 1,
    @ItemsPerPage int = 2,
    @IDAlbum int,
    @TotalRows int out)
AS
BEGIN
  SET NOCOUNT ON
  DECLARE
    @StartIdx int,
    @SQL nvarchar(max),  
    @SQL_Conditions nvarchar(max),
    @EndIdx int

    IF @PageNo < 1 SET @PageNo = 1
    IF @ItemsPerPage < 1 SET @ItemsPerPage = 10

    SET @StartIdx = (@PageNo -1) * @ItemsPerPage + 1
    SET @EndIdx = (@StartIdx + @ItemsPerPage) - 1
    SET @SQL = 'SELECT picture
                FROM (
                SELECT  ROW_NUMBER() OVER(ORDER BY IDAlbum) AS Row, *  
                      FROM  Album_Pic WHERE IDAlbum =  @IDAlbum )
                      AS tbl WHERE  
                      Row >= ' + CONVERT(varchar(9), @StartIdx) + 
                      ' AND Row <=  ' + CONVERT(varchar(9), @EndIdx) 



    EXEC sp_executesql @SQL

    SET @SQL = 'SELECT @TotalRows=COUNT(*) FROM Album_Pic WHERE IDAlbum = ' + CONVERT(varchar(9), @IDAlbum) 
    EXEC sp_executesql 
        @query = @SQL, 
        @params = N'@TotalRows INT OUTPUT', 
        @TotalRows = @TotalRows OUTPUT 
END
 

----------------------------------------------------

Merci d'avance

Hadil

EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ICI

Merci d'y penser dans tes prochains messages.

2 réponses

  1. Hadil6 Messages postés 8 Date d'inscription   Statut Membre Dernière intervention   1
     
    Le pbm est resolu.
    merci.
    0
    1. Utilisateur anonyme
       
      Peux tu partager la solution pour un autre à l'avenir ?
      0
  2. Hadil6 Messages postés 8 Date d'inscription   Statut Membre Dernière intervention   1
     
    La solution est la suivante:

    Comme elle s'agit d'une requêtes (SQL) dynamiques, il faut remplacer @IDAlbum par convert(varchar(10),@IDAlbum) lors de la valorisation de IDAlbum dans la table Album_Pic.

    Ci dessous la partie changee dans le code:

    SET @SQL = 'SELECT picture
    FROM (
    SELECT ROW_NUMBER() OVER(ORDER BY IDAlbum) AS Row, *
    FROM Album_Pic WHERE IDAlbum = ' +convert(varchar(10),@IDAlbum)+' )
    AS tbl WHERE
    Row >= ' + CONVERT(varchar(9), @StartIdx) +
    ' AND Row <= ' + CONVERT(varchar(9), @EndIdx)
    0