Solved

Must declare the scalar variable

Posted on 2011-09-13
7
1,344 Views
Last Modified: 2012-05-12
The sp that receives the Table name as a parameter.
I keep receiving the error message

Must declare the scalar variable "@ProdID".

I tired many different ways and didn't work.

Please help.

--------------------------------------------------
USE [MyDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_Test]
      -- Add the parameters for the stored procedure here
      @schema sysname,
      @table_name sysname,
      @ProdID varchar(10),
      @EntryDateTime  datetime,
      @SeqNo bigint,
      @File_path varchar (250)
AS
BEGIN
SET @table_name = RTRIM(@table_name)
DECLARE @cmd AS NVARCHAR(max)

SET NOCOUNT ON
SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (@ProdID, @EntryDateTime, SYSDATETIME(),  @SeqNo, @File_path)'
print @cmd

EXEC sp_executesql @cmd
END

GO
-------------------------------------------------------------------
0
Comment
Question by:pcssecure
  • 4
  • 3
7 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 36528839
Hi,

You're trying to use the variable in the dynamic sql, that is not possible. Either use the value directly

SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (''' +@ProdID + ''', ''' +@EntryDateTime + ''', ''' +SYSDATETIME() + ''', ''' +@@SeqNo + ''', ''' +@@File_path + ''')'

or use

sp_executesql

/peter
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 36528851
Looking at the datatypes, the line should read:

SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (''' +@ProdID + ''', ''' +CONVERT(varchar, @EntryDateTime, 101) + ''', ''' +CONVERT(varchar, SYSDATETIME(), 101) + ''', ' +CONVERT(varchar, @SeqNo) + ', ''' +@File_path + ''')'
0
 

Author Comment

by:pcssecure
ID: 36529519
Thanks for the help but still not working from my VB app. But it is working from SQL Server Management Studio. So I guess we are very close to the solution.

            Dim EntryTime As SqlParameter = New SqlParameter("@EntryTime ", SqlDbType.DateTime)
            EntryTime .Value =In_ EntryTime 'Passed in as parameter type DateTime
            myCommand.Parameters.Add(EntryTime )

Error : Conversion failed when converting date and/or time from character string.

Not sure why should I use convert when I pass in value as Type DateTime?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 22

Expert Comment

by:pivar
ID: 36531851
Dim EntryTime As SqlParameter = New SqlParameter("@EntryTime ", SqlDbType.DateTime) should be
Dim EntryTime As SqlParameter = New SqlParameter("@EntryDateTime ", SqlDbType.DateTime)?

I think the error comes from sql, but I can't see anything obvious, so can you log what the content of @cmd is?
0
 

Author Comment

by:pcssecure
ID: 36533965
Thanks. Problem solved but strange. I need to capture up to milliseconds. So when I use

SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (''' +@ProdID + ''', ''' +CONVERT(varchar, @EntryDateTime, 121) + ''','''+ CONVERT(varchar, SYSDATETIME(),121) + ''', ' +CONVERT(varchar, @SeqNo) + ', ''' +@File_path + ''')'
Produced Error : Conversion failed when converting date and/or time from character string.

So I changed to

SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (''' +@ProdID + ''', ''' +CONVERT(varchar, @EntryDateTime, 121) + ''', SYSDATETIME() , ' +CONVERT(varchar, @SeqNo) + ', ''' +@File_path + ''')'
And it works fine.
Any idea why caused an error in the first one when using 121 with SYSDATETIME()?
Is using 121 correct in convert function to get up to millisecond?
I got the working one by trial and error.
Thanks.
0
 

Author Comment

by:pcssecure
ID: 36533972
Thanks and hope to here from you.
0
 
LVL 22

Expert Comment

by:pivar
ID: 36534598
I'm guessing now, but it could be that the problem lies in that SYSDATETIME returns a DATETIME2.
You should see ms with 121 (yyyy-mm-dd hh:mi:ss.mmm(24h))
Have you tried another format, perhaps 109 or 113? Or try to convert to DATETIME first?
I'm can not test it myself right now.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
simple shopping cart database structure 5 73
update joined tables 2 55
insert row field data graphically 4 28
Currency in SQL? 2 31
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question