Solved

exec with string + variable

Posted on 2013-06-12
5
235 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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

17 Experts available now in Live!

Get 1:1 Help Now