Solved

exec with string + variable

Posted on 2013-06-12
5
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

759 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