Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 11th, 2003, 08:12 PM
lin lin is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 1 lin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
TWO VARIABLES in stored procedure

My procedure gives me a hard time
I cant use the variable @col to be a column name to compara to another variable (@val) - anyone´s got the answer?!

Lin

CREATE PROCEDURE searchStudent(@col VARCHAR(50),@val VARCHAR(50),@type VARCHAR(20))AS

BEGIN TRANSACTION


IF @type = 'all' BEGIN

CREATE TABLE #temp
(
namn varchar(50),
adress varchar(50),
telnr varchar(50),
studenttyp varchar(20)
)

INSERT INTO #temp
SELECT g.namn,g.adress,g.telnr, s.studenttyp
FROM gast as g, student as s
WHERE s.stuid = g.stuid

INSERT INTO #temp
SELECT s.namn,s.adress,s.telnr, t.studenttyp
FROM svensk as s, student as t
WHERE s.stuid = t.stuid


SELECT *
FROM #temp
WHERE @col LIKE @val + '%'

END
ELSE IF @type = 'swed' BEGIN

SELECT *
FROM svensk
WHERE @colLIKE @val + '%'

END
ELSE IF @type = 'gues' BEGIN

SELECT *
FROM gast
WHERE @col LIKE @val + '%'
END



COMMIT

Reply With Quote
  #2  
Old October 12th, 2003, 01:57 PM
rdoekes rdoekes is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Strasbourg, France
Posts: 181 rdoekes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 7
Send a message via AIM to rdoekes Send a message via Yahoo to rdoekes
You cannot use the @col variable that way. What you could do is write dynamic SQL and call the sp_executesql system stored proc to execute the sql statement.
Code:
DECLARE @sql nvarchar(1000)
SET @sql = N'SELECT * ' + CHAR(13) +
N'FROM #temp ' + CHAR(13) +
N' WHERE ' +  @col + N' LIKE ' + @val + N'%' 

EXECUTE  sp_executesql @sql

etc.

Hope this helps
__________________
- Rogier Doekes

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > TWO VARIABLES in stored procedure


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT