May 12, 2009 at 6:51 am
Hi to everyone
I am using ms sql 2000 db i am retriving data at run time so i have used T-sql select statement.when retriving data 50% of my code is givng output i.e., it retriving spaceids of my user at run time but it is failing to get the colums like country,state,city pls help me to retive country,state,city also.
Thanx in advance this is my code
DECLARE @username varchar(30), @spaceid INT
DECLARE @s-2 VARCHAR(1000), @r varchar(1000)
set @username='vinay@gmail.com'
set @spaceid =''
set @s-2 = 'select distinct spaceid from staging.dbo.siteownerspaces where username = ' + quotename(@username, '''')
set @r = 'select country,state,city from staging.dbo.' + quotename('myadvertises'+ cast(@spaceid as varchar(30)),'''')
exec(@s)
exec(@r)
May 12, 2009 at 8:18 am
May 12, 2009 at 8:35 am
Please give us some sample data so we can better help you:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 13, 2009 at 4:28 am
Hi everybody after searching a lot i have used the following code to retrive the logged users information the below code is working for only one record i.e., its retriving only one spaceid i have 8 spaceids in my db any suggestions how to retrive all records
DECLARE @username varchar(30), @spaceid INT
DECLARE @s-2 VARCHAR(1000), @r varchar(1000)
set @username='vinay@gmail.com'
Select distinct @spaceid =spaceid from staging.dbo.siteownerspaces where username = @username
set @r = 'select ''' + cast(@spaceid as varchar(30)) + ''' As SpaceId, country,state,city from staging.dbo.[myadvertises'+ cast(@spaceid as varchar(30)) + ']'
exec(@r)
May 13, 2009 at 5:13 am
Check if this code works for you?
DECLARE @username varchar(30),
@s-2 VARCHAR(MAX)
SELECT@username = 'vinay@gmail.com'
SELECT@s-2 = ISNULL( @s-2 + CHAR(10) + 'UNION ALL' + CHAR(10), '' )
+ 'SELECT''' + CONVERT( VARCHAR(30), spaceid ) + ''' AS SpaceID, country, state, city '
+ 'FROMstaging.dbo.[myadvertises'+ CONVERT( VARCHAR(30), spaceid ) + ']'
FROM(
SELECTDISTINCT spaceid
FROMstaging.dbo.siteownerspaces
WHEREusername = @username
) O
PRINT @s-2
--EXECUTE( @s-2 )
--Ramesh
May 13, 2009 at 5:30 am
Hi Ramesh Thank u very much ur guys r so great its working great with little modification its came with execute(@s) but i didnt understand some code especially at
FROM (
SELECT DISTINCT spaceid
FROM staging.dbo.siteownerspaces
WHERE username = @username
) O
so please explain the code
Thank u very much once again
May 13, 2009 at 5:45 am
I am glad that it worked well.
Now for the explanation, this section
FROM (
SELECT DISTINCT spaceid
FROM staging.dbo.siteownerspaces
WHERE username = @username
) O
is called as "Derived Table". A derived table is just a normal table except that it is used when you want to select calculated/computed columns, filter rows, re-use the calculated columns in main query etc.
Here, the purpose of the sub-query is to get DISTINCT spaceids from the table, which is further used to build a dynamic SQL.
--Ramesh
May 13, 2009 at 5:55 am
Thanx a lot for ur explanation.
May 13, 2009 at 7:16 am
Hi Ramesh i have little problem in the colum of state i have some null values in the place of the null values i want to get 'false' so i have tried this ISNULL(state,'false') AS state but its not working any suggestions
May 13, 2009 at 7:46 am
Add the following in place of "state"
ISNULL( NULLIF( LTRIM( RTRIM( [state] ) ), '' ), 'false' )
--Ramesh
May 13, 2009 at 7:54 am
Hi thanx for ur response i have added ur code
ISNULL( NULLIF( LTRIM( RTRIM( [state] ) ), '' ), 'false' )
in the place of state its giving error that
"Incorrect syntax near the keyword 'all' "
May 13, 2009 at 7:58 am
Can you post the exact query that gives you this error?
--Ramesh
May 13, 2009 at 8:03 am
DECLARE @username varchar(30),
@s-2 VARCHAR(MAX)
ok this is the query
SELECT @username = 'kiranyadav.n@gmail.com'
SELECT @s-2 = ISNULL( @s-2 + CHAR(10) + 'UNION ALL' + CHAR(10), '' )+ 'SELECT ''' + CONVERT( VARCHAR(30), spaceid ) + ''' AS SpaceID, country,ISNULL( NULLIF( LTRIM( RTRIM( [state] ) ), '' ), 'all' ),city,fromdate,todate,cost '+ 'FROM staging.dbo.[myadvertises'+ CONVERT( VARCHAR(30), spaceid ) + ']'
FROM (
SELECT DISTINCT spaceid
FROM staging.dbo.siteownerspaces
WHERE username = @username
) O
--PRINT @s-2
EXECUTE( @s-2 )
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply