You are on page 1of 2

set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON
go

/*=============================================================================
Autore: Andrea Vincenzi Nome: sp_lib_lookupstrin
g
Esegue una lookup parametrizzata
Notes / History
01/11/2004
creata la procedura .
NB: Non funziona, è da mettere a punto la sintassi per usare la executesql.
=============================================================================*/
ALTER PROCEDURE [dbo].[sp_lib_lookupstring]
@p_tablename Desc50 = NULL
,@p_keyname Desc20 = NULL
,@p_keyval ninteger = NULL
,@p_descname Desc20 = NULL
,@p_descval Desc50 = NULL O
UTPUT
,@p_return_msg Desc50 = NULL OUTPUT
,@p_status ninteger = NULL O
UTPUT
AS
SET XACT_ABORT OFF -- Turn off auto abort on errors
SET NOCOUNT ON -- Turn off row count messages
--=====================================================
-- Dichiara le variabili locali
--=====================================================
DECLARE
@v_return_status integer -- Update result status
, @v_row_count integer -- General row count
, @v_rec_inseriti integer -- Numero records inseri
ti
, @v_rec_aggiornati integer -- Numero records aggiornati
, @v_rec_letti integer -- Numero records letti
, @s varchar(200) -- Variabile di appoggio
, @RC integer
set @v_rec_letti = 0
/*=============================================================================
Controllo parametri di input
===============================================================================*
/
IF @p_tablename IS NULL
BEGIN
SET @p_return_msg = 'Parametro di input @p_tablename non presente'
SET @p_status = -1
RETURN -1
END
IF @p_keyname IS NULL
BEGIN
SET @p_return_msg = 'Parametro di input @p_keyname non presente'
SET @p_status = -1
RETURN -1
END
IF @p_keyval IS NULL
BEGIN
SET @p_return_msg = 'Parametro di input @p_keyval non presente'
SET @p_status = -1
RETURN -1
END
IF @p_descname IS NULL
BEGIN
SET @p_return_msg = 'Parametro di input @p_descname non presente'
SET @p_status = -1
RETURN -1
END
/*=============================================================================
Lettura valore
===============================================================================*
/
Declare @SQL nVarChar(1000)
SELECT @SQL = 'SELECT ' + @p_descname + ' FROM ' + @p_tablename
SELECT @SQL = @SQL + ' WHERE ' + @p_keyname + ' = ' + @p_keyval
Exec sp_executesql @SQL, N'@p_tablename Desc50', @p_tablename = 'DCORRIE
RE'
BEGIN TRANSACTION
SELECT
@p_descval = @p_descname
FROM DCORRIERE
WHERE
@p_keyname = @p_keyval
/*=============================================================================
Controllo successo operazione
===============================================================================*
/
SELECT
@v_return_status = @@ERROR
,@v_row_count = @@ROWCOUNT
SET @v_rec_letti = @v_rec_letti + @v_row_count
IF @v_return_status <> 0
BEGIN
SET @p_return_msg = 'Errore in inserimento parametro'
SET @p_status = -1
ROLLBACK
RETURN -1
END
IF @v_row_count = 0
BEGIN
SET @p_return_msg = 'Parametro non trovato'
SET @p_status = -1
ROLLBACK
RETURN -1
END
ELSE
BEGIN
SET @p_return_msg = 'OK'
SET @p_status = 0
COMMIT
RETURN 0
END

You might also like