We help IT Professionals succeed at work.
Get Started

Getting error when using sp_executesql

564 Views
Last Modified: 2009-12-16
Will someone please tell me why i get this error:
"Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'."

When i execute this simple stored procedure to get the last number (ID) loaded into a column of a table. I'm doing this because the primary key of a lot of tables in my project dont autoincrement

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetLastId]
      -- Add the parameters for the stored procedure here
@table_name nvarchar(30),
@column_name nvarchar(30)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
SET NOCOUNT ON;
      declare @sp_exec varchar(200)

      set @sp_exec = N'select top 1 @column_name_in from @table_name_in order by @column_name_ in desc'

            -- Insert statements for procedure here
      EXECUTE sp_executesql  @sp_exec, N'@table_name_in nvarchar, @column_name_in nvarchar',
                                          @table_name, @column_name                        

END


Comment
Watch Question
CERTIFIED EXPERT
Awarded 2008
Awarded 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE