Solved

Must declare the scalar variable

Posted on 2011-09-13
7
1,340 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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