CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2005
    Location
    Spain!
    Posts
    149

    Unhappy [RESOLVED] SQL 2005 - Stored procedure parameter problem

    Hi all!, I've a SQL Express 2005 database that have many tables with AFTER INSERT/UPDATE triggers that does the same on all the tables.

    Then, i've created an StoredProcedure to be called within these triggers.. so less repeteable code and an advantage on future changes..

    The stored proc:
    Code:
    CREATE PROCEDURE [dbo].[sp_ControlarModificacion] 
    	-- Add the parameters for the stored procedure here
    	@Tabla table, 
    	@Clave int
    AS
    BEGIN
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
    	update @Tabla set FechaModificacion = GETDATE(), UsuarioModificacion = USER where clave = @clave
    END
    And the call from the trigger will be some like..

    Code:
    exec dbo.sp_ControlarModificacion inserted, @Clave
    But i've a problem, SQL Server throw me the error "Incorrect syntax near the keyword 'table "when i try to save the stored procedure.

    Some ideas? Thanks!
    Last edited by satanorz; July 18th, 2008 at 06:22 AM. Reason: Solved ;)

  2. #2
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: SQL 2005 - Stored procedure parameter problem

    I do not think you can parse a table as input to a SPROC in SQL Server 2005 (or earlier).

    You'll have to parse the specific parameters in, instead of the table.

  3. #3
    Join Date
    Aug 2005
    Location
    Spain!
    Posts
    149

    Re: SQL 2005 - Stored procedure parameter problem

    The problem is that i need the parameter to be a table, because i need to make an update on it :S

  4. #4
    Join Date
    Aug 2005
    Location
    Spain!
    Posts
    149

    Wink Re: SQL 2005 - Stored procedure parameter problem

    Solved!

    I've found a solution for my problem googling for a while.

    Instead of passing the table, i pass only the name of it.
    Then, i create a nvarchar variable to build the query using the table name parameter..

    Finally we can run the query using the storedproc sp_executesql

    An example acording my problem..

    Code:
    CREATE PROCEDURE [dbo].[sp_ControlarModificacion] 
    	-- Add the parameters for the stored procedure here
    	@Tabla varchar(50), 
    	@Clave int
    AS
    BEGIN
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
    
    	DECLARE @Consulta nvarchar(200)
    
    	SET @Consulta = 'update ' + @Tabla + ' set FechaModificacion = GETDATE(), UsuarioModificacion = USER where clave = ' + str(@Clave)
    	exec sp_executesql @Consulta
    END
    Hope it helps someone that have the same problem!

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: SQL 2005 - Stored procedure parameter problem

    i would never do this.. the performance implications are horrendous
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured