September 30, 2003 at 2:40 pm
need help how can i do this ???
--------------
DECLARE @mh-2 [varchar](10)
DECLARE @mha[varchar](50 )
set @mh-2='1,2,3'
set @mha='(where mhlkot.mhlka_id IN (@mh))'
SELECT TOP 100 PERCENT dbo.SilokE.Fname, dbo.SilokE.id, dbo.mhlkot.mhlka, dbo.mhlkot.mhlka_id
FROM dbo.SilokE INNER JOIN
dbo.mhlkot ON dbo.SilokE.mhlka = dbo.mhlkot.mhlka_id
exec ('@mha')
----------------------------------------
thnks
ilan
September 30, 2003 at 3:44 pm
DECLARE
@sqlCmd NVARCHAR(1000),
@mh-2 VARCHAR(10),
@mha VARCHAR(50)
SET @mh-2 = '1,2,3'
SET @mha = 'WHERE mhlkot.mhlka_id IN (' + @mh-2 + ')'
SET @sqlCmd = N'SELECT TOP 100 PERCENT' +
' dbo.SolokE.Fname, dbo.SilokE.id,' +
' dbo.mhlkot.mhlka, dbo.mhlkot.mhlka_id' +
' FROM dbo.SilokE INNER JOIN dbo.mhlkot ON dbo.SilokE.mhlka = dbo.mhlkot.mhlka_id' + @mha
sp_executesql @sqlCmd
There may be typoes here...
Guarddata-
Edited by - guarddata on 09/30/2003 3:44:53 PM
September 30, 2003 at 4:08 pm
thnks but i get an error
-------------
Server: Msg 170, Level 15, State 1, Line 11
Line 11: Incorrect syntax near 'sp_executesql'.
-------------------------------------
ilan
September 30, 2003 at 4:12 pm
Assuming you are in Sql2000, you will need the line to read
EXEC sp_executesql @sqlCmd. I always forget EXEC in front
If you are in Sql 7.0, you may need EXECUTE( @sqlCmd)
Guarddata-
September 30, 2003 at 4:24 pm
still an error
-----------------
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'mhlkot'.
------------------------------------------
thnks
ilan
September 30, 2003 at 4:33 pm
Pardon me
it is ok the code
---------------------
thnks
ilan
September 30, 2003 at 4:34 pm
Looks like the first column was names Solok instead of Silok...
Just for curiosity - how many rows are in SilokE and mhlkot?
Guarddata-
September 30, 2003 at 4:43 pm
ok
----------
in siloke =(59 row(s) affected)
and in
mhlkot= (10 row(s) affected)
-----------
ilan
September 30, 2003 at 4:48 pm
watch this it work like this !!!
-------------------------------------
DECLARE
@sqlCmd NVARCHAR(1000),
@mh-2 VARCHAR(10),
@mha VARCHAR(50)
SET @mh-2 = '1'
SET @mha = 'WHERE mhlkot.mhlka_id IN ('+ @mh-2 +')'
SET @sqlCmd = N'SELECT dbo.SilokE.Fname,dbo.SilokE.id, dbo.mhlkot.mhlka
FROM dbo.SilokE INNER JOIN
dbo.mhlkot ON dbo.SilokE.mhlka = dbo.mhlkot.mhlka_id
' + @mha
EXEC sp_executesql @sqlCmd
-------------------------------
ilan
October 1, 2003 at 3:55 pm
How about using a temp/variable table to hold the values you want in your IN clause and doing away with the sp_executesql completely?
DECLARE @foo table(mhlka_id int)
insert into @foo values(1)
insert into @foo values(2)
insert into @foo values(3)
SELECT E.Fname, E.id, M.mhlka, M.mhlka_id
FROM dbo.SilokE E
JOIN mhlkot M ON E.mhlka = M.mhlka_id
JOIN @foo f ON M.mhlka_id = f.mhlka_id
Gary Johnson
Microsoft Natural Language Group
DBA, Sr. DB Engineer
Gary Johnson
Microsoft Natural Language Group
DBA, Sr. DB Engineer
This posting is provided "AS IS" with no warranties, and confers no rights. The opinions expressed in this post are my own and may not reflect that of my employer.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply