Solved

Must declare the scalar variable

Posted on 2011-09-13
7
1,330 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now