?
Solved

I am looking for a way to concatanate parameters

Posted on 2011-10-31
6
Medium Priority
?
163 Views
Last Modified: 2012-05-12
Hello, I am looking for a way to concatanate parameters in the following way:

@cname as varchar(6),
@mname as varchar(5)

DECLARE @cmname as varchar(12)

How would I concatanate @cname + '-' + @mname and send the values to @cmname?

I do not want the value of cmname to be changed by the user of the query or program but I would like that value of @cmname to be stored as a parameter to be used in a query that I am writing.  How would I do this?
0
Comment
Question by:VBBRett
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:dhruvpratap
ID: 37058521
You should write something like this:
CREATE PROCEDURE TEST(IN str1 varchar(10), IN str2 varchar(10), IN str3 varchar(10)) 
BEGIN 
SET @query = CONCAT("SELECT fieldname1, fieldname2 from tablename WHERE ", str1, str2, str3); 
PREPARE statement FROM @query; 
EXECUTE statement; 
END

Open in new window


Reference : http://forums.mysql.com/read.php?98,264282,264292#msg-264292
0
 

Author Comment

by:VBBRett
ID: 37058653
OK, so how would you declare a local variable and hold data but not have to receive input from a user in sql server?
0
 
LVL 1

Expert Comment

by:dhruvpratap
ID: 37058690
I think you didn't get my point. Try using this.

SET @cmname = CONCAT(cname,'-',mname)

Open in new window

0
Industry Leaders: 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!

 

Author Comment

by:VBBRett
ID: 37058777
set did not work for some reason and concat is not a recognized term.  Are you using sql server?
0
 
LVL 5

Accepted Solution

by:
eridanix earned 2000 total points
ID: 37119367
Hi,

to set value to @cmname, you can use this:
DECLARE @cmname as varchar(12)

SET @cmname = @cname + '-' + @mname

Open in new window

0
 

Author Closing Comment

by:VBBRett
ID: 37119579
Thank you very much!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

571 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