Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 29
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    162

    [SQL SERVER] Error converting data type varchar to int

    Salve, spero sia giusto il posto in cui sto postando questa nuova discussione e spero soprattutto che mi possiate aiutare.

    Ho una store proceudre che mi restituisce quell'errore sopra in oggetto.

    Allora ho una store procedure che prende dei parametri in ingresso, tra questi parametri c'è una variabile(@query varchar(1000)) dove prende un insieme di valori(es.62,74,56,67)in modo che nella condizione where gli vado a fare che IDUser in (@query);

    Qui di seguito c'è la fine della store procedure:
    [CODE]



    SELECT Ler.IDLeaveRequest,
    Ler.IDUser,
    Ler.IDLeaveType,
    dbo.DataToString(Ler.leaFromDate) as leaFromDate,
    dbo.DataToString(Ler.leaTODate) as leaToDate,
    Ler.leaAmount,
    Ler.leaAmountBaseUnit,
    Ler.leaReason,
    Ler.leaContactAddress,
    Ler.leaContactPhone,
    Ler.leaApproval,
    isnull(Ler.IDApprovalManager, -1) as IDApprovalManager,
    Ler.IDStatus,
    Ler.leaManagerNote
    FROM leaveRequest Ler

    WHERE Ler.IDUser = ISNULL( @IdUser , Ler.IDUser)
    AND Ler.IDLeaveType = ISNULL(@IdLeaveType , Ler.IDLeaveType)
    AND Ler.leaFromDate >= @DateStart
    AND Ler.leaTODate <= '@DateEnd
    AND Ler.IDStatus = ISNULL(@IdStatus , Ler.IDStatus)
    AND ((Ler.IDApprovalManager = ISNULL( @IdApprovalManager, Ler.IDApprovalManager))
    or ( @IdApprovalManager is null and Ler.IDApprovalManager is null))
    AND Ler.IDUser IN ( @query ) )


    END
    [CODE]

    Vi prego aiutatemi non so più dove sbattere la testa

    Grazie

  2. #2
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    beh..la prima cosa che credo non vada bene è l'apice...qui
    AND Ler.leaTODate <= '@DateEnd

    posta l'intera stored procedure e non solo la fine. chi ti dice che l'errore non si verifica all'inizio della sp?
    I got the remedy

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    162
    Innaznitutto ti ringrazio di vero cuore per avermi risposto.
    Qui di seguito ti posto tutta la storer procedure:

    codice:
    USE [BSPF_GLOB]
    GO
    /****** Object:  StoredProcedure [dbo].[spLeaveRequestList2]    Script Date: 02/26/2009 14:59:40 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[spLeaveRequestList2]
     @IdUser    int,
     @IdResponsible  int,
     @IdLeaveType  int,
     @LeaFromDate  varchar(14),
     @LeaToDate   varchar(14),
     @IdStatus   int,
     @IdApprovalManager  int,
     @query varchar(1000)
    AS
    BEGIN
    
     SET NOCOUNT ON;
    
     DECLARE @DateStart datetime
     DECLARE @DateEnd   datetime 
    
     IF @IdUser = -1
      SET @IdUser = 0
     
     IF @IdResponsible = -1
      SET @IdResponsible = 0
    
     IF @IdLeaveType = -1
      SET @IdLeaveType = 0
    
     IF @LeaFromDate = ''
       SET @LeaFromDate ='19000101000000'
    
     IF @LeaToDate = ''
      SET @LeaFromDate ='21001231000000'
     
    
    IF @IdStatus = -1
      SET @IdStatus = 0
    
     IF @IdApprovalManager = -1
      SET @IdApprovalManager = 0
       
       SET @DateStart = dbo.StringToData(@LeaFromDate)
    	SET @DateEnd   = dbo.StringToData(@LeaToDate)
    
    
    IF @QUERY IS NULL
    
      SELECT  Ler.IDLeaveRequest, 
       Ler.IDUser, 
       Ler.IDLeaveType, 
       dbo.DataToString(Ler.leaFromDate) as leaFromDate, 
       dbo.DataToString(Ler.leaTODate) as leaToDate, 
                Ler.leaAmount, 
       Ler.leaAmountBaseUnit, 
       Ler.leaReason, 
       Ler.leaContactAddress, 
                Ler.leaContactPhone, 
       Ler.leaApproval, 
       isnull(Ler.IDApprovalManager, -1) as IDApprovalManager,
       Ler.IDStatus, 
                Ler.leaManagerNote
      FROM   leaveRequest Ler 
         
        WHERE   Ler.IDUser     =  ISNULL(@IdUser, Ler.IDUser)
       AND   Ler.IDLeaveType       =  ISNULL(@IdLeaveType, Ler.IDLeaveType)
       AND   Ler.leaFromDate    >= @DateStart   
       AND Ler.leaTODate         <= @DateEnd
       AND Ler.IDStatus    =  ISNULL(@IdStatus, Ler.IDStatus)
       AND ((Ler.IDApprovalManager =  ISNULL(@IdApprovalManager, Ler.IDApprovalManager))
       or (@IdApprovalManager is null and Ler.IDApprovalManager is null))
      
    
    
    
     ELSE 
    
    
      exec (' SELECT  Ler.IDLeaveRequest, 
       Ler.IDUser, 
       Ler.IDLeaveType, 
       dbo.DataToString(Ler.leaFromDate) as leaFromDate, 
       dbo.DataToString(Ler.leaTODate) as leaToDate, 
       Ler.leaAmount, 
       Ler.leaAmountBaseUnit, 
       Ler.leaReason, 
       Ler.leaContactAddress, 
       Ler.leaContactPhone, 
       Ler.leaApproval, 
       isnull(Ler.IDApprovalManager, -1) as IDApprovalManager,
       Ler.IDStatus, 
                Ler.leaManagerNote
      FROM   leaveRequest Ler 
         
        WHERE   Ler.IDUser     =  ISNULL(' + @IdUser +', Ler.IDUser)
        AND   Ler.IDLeaveType       =  ISNULL(' + @IdLeaveType + ', Ler.IDLeaveType)
        AND   Ler.leaFromDate    >= ' + @DateStart +'   
        AND Ler.leaTODate         <= '+ @DateEnd + '
        AND Ler.IDStatus    =  ISNULL(' + @IdStatus +', Ler.IDStatus)
        AND ((Ler.IDApprovalManager =  ISNULL(' + @IdApprovalManager + ', Ler.IDApprovalManager))
        or (' + @IdApprovalManager + ' is null and Ler.IDApprovalManager is null))
        AND   Ler.IDUser IN (' + @query + ')
    
    
    ')
    
    END
    Non lo postata prima perchè pensavo fosse troppo lunga da inserire in una discussione.

    Cmq tornando a noi sopra c'e una function (dbo.StringToData(@LeaFromDate)) che appunto fa una trasformazione da stringa a data prendendo le sottostringhe.
    il problema è nelle date all'interno dell'exec!
    mi dà errore Incorrect Syntax near '1' che da quanto ho potuto vedere è il numero della prima data.

    Grazie ancora

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    162
    Nessuno mi potrebbe dare un mano con questo probelma che dura ormai da due giorni?

    Grazie ancora

  5. #5
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    mmmh.. :master:
    mah dbo.StringToData(@LeaFromDate) è una funzione che hai creato tu?
    I got the remedy

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    162
    Si, per sicurezza te la faccio vedere,

    codice:
    LTER FUNCTION [dbo].[StringToData]
    (
    	@Appo	varchar(20)
    )
    RETURNS datetime
    AS
    BEGIN
    	
    	DECLARE @Data datetime
    
    	SET @Appo = Substring(@Appo,1,4)  + '-' + Substring(@Appo,5,2)  + '-' + 
                    Substring(@Appo,7,2) + ' ' + Substring(@Appo,9,2)  + ':' + 
    				Substring(@Appo,11,2) + ':' + Substring(@Appo,13,2)  
    
    	SET @Data = convert(datetime, @Appo, 120) 
    
    	-- Return the result of the function
    	RETURN @Data
    
    END

    Ma il problema non se hai provato a simulare questa store è nella seconda select che eseguo nell'exec e più precisamente mi dà l'errore nelle due date, infatti se io commento le due date mi funziona.
    Grazie ancora spero mi potrai essere utile!

  7. #7
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    ma sei sicuro che questa funzione faccia quello che deve fare?
    cioè fammi un esempio con una data e quello che dovrebbe produrre in uscita...
    I got the remedy

  8. #8
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    162
    Niente prende una data formato varchar (19000101000000) e la trasforma in Datetime(1900-01-01 00:00:00)
    questa è quello che deve fare.

    ti ripeto che nella prima select della store procedure mi dà i risultati dovuti è nella seconda che, mi segnala errore in prossimità del valore 1 che se non ho capito male è il secondo uno della data sopra citata.

    ma non cApisco il perchè!
    P.S.: se tu hai alternative a questa store procedure, me li potresti far sapere, nel senso se tu la faresti in maniera diversa.

  9. #9
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    Originariamente inviato da sanfra1976
    Niente prende una data formato varchar (19000101000000) e la trasforma in Datetime(1900-01-01 00:00:00)
    questa è quello che deve fare.
    va bene cosi' com'è!
    ma se provi a togliere
    AND Ler.IDUser IN (' + @query + ')

    ti funziona? mi viene da pensare che quella @query ha qualcosa che non và
    I got the remedy

  10. #10
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    162
    A me funziona tutto commentando le due Date all'interno della seconda select, se lascio le due date si blocca sulla prima data mi segnala errore in prossimità di "1".


    E come faresti tu, avendo un elenco di id(@query)sotto un altro id dove quest'ultimo deve controllare se nell'elenco di id ci sono dei sottoid?

    Mi sembra la cosa più logica.Il problema ripeto non è questo penso, ma qualcosa all'interno di queste function oppure qualcos'altro all'interno dell'exec.!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.