Solved

exec with string + variable

Posted on 2013-06-12
5
242 Views
Last Modified: 2013-06-13
Hi there,

This code (below) does not work, although the sproc is perfect... I am SURE the problem is in the bold part. How should I correct it?

(It must insert a certain "Package Number 14" to the student with the email given)

Thanks!

(I know a variable would NOT be needed here, but with this correction I will put a WHILE thing to happen afterwards, therefore please help as requested.)

--------------------------------------------

USE [dbName]
GO

DECLARE      
@return_value int,
@PackNumber int

SET @PackNumber = 14

EXEC      @return_value = [dbo].[insMaterial_sproc]
            @EmailLogin = N'emailgiven_thisOk',
            @MaterialName = 'Package Number'+@PackNumber

SELECT      'Return Value' = @return_value
GO
0
Comment
Question by:fskilnik
5 Comments
 
LVL 4

Expert Comment

by:BAKADY
ID: 39242861
try this, maybe help... if not please post your error message too...
EXEC      @return_value = [dbo].[insMaterial_sproc]
            @EmailLogin = N'emailgiven_thisOk',
            @MaterialName = 'Package Number ' + cast(@PackNumber as varchar(max))

Open in new window

regards
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 500 total points
ID: 39242875
You can't have any "function" as a part of an EXEC call.  You also can't concatenate a string and an INT without a CAST.  Combine the string beforehand:

USE [dbName]
GO

DECLARE      
@return_value int,
@PackNumber int,
@PackNumString VARCHAR(100)


SET @PackNumber = 14;
SET @PackNumString = 'Package Number' + CAST(@PackNumber AS VARCHAR(40));

EXEC      @return_value = [dbo].[insMaterial_sproc]
            @EmailLogin = N'emailgiven_thisOk',
            @MaterialName = @PackNumString

SELECT      'Return Value' = @return_value
GO 

Open in new window

0
 
LVL 23

Expert Comment

by:nemws1
ID: 39242884
Also, this line doesn't make any sense:
SELECT      'Return Value' = @return_value

Open in new window


You probably want this instead:

SELECT      'Return Value: ' + CAST(@return_value AS VARCHAR(40))

Open in new window

0
 
LVL 2

Expert Comment

by:vivekkumarSharma
ID: 39243615
@MaterialName = 'Package Number'+str(@PackNumber)
0
 

Author Comment

by:fskilnik
ID: 39246293
Thanks for you all. (Sorry for the delay, too busy...)

@BAKADY: error message - "Incorrect syntax near '+'."

@nemws1: PERFECT (and great explanation, thank you)

(On the SELECT matter: the original line was not changed, and it worked anyway, but thanks for this additional suggestion)

@vivekkumarSharma: error message - "Incorrect syntax near '+'."
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

831 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