Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Creating 1 Select Query without Cursor, Function, temp object or Loop Expand / Collapse
Author
Message
Posted Wednesday, December 26, 2012 2:41 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 6:24 PM
Points: 63, Visits: 302
Hi All

I have a query that need to Rewrite it without cursor, function or loop to produce same result and should be included in ONE SELECT (no temporary object involved).

Code.............................................
declare @ColName sysname,
@TableName sysname,
@FieldList varchar(MAX),
@LastTableName sysname

select @LastTableName = '',
@FieldList = ''

declare @tb table (TableName sysname null, FieldList varchar(MAX) null)
declare curs cursor for

select c.name, object_name(c.object_id)
from sys.columns c INNER JOIN sys.objects o on c.object_id = o.object_id
where o.type = 'U'
order by o.object_id

open curs
fetch curs into @ColName, @TableName
set @LastTableName = @TableName
while @@FETCH_STATUS = 0

BEGIN
if @LastTableName <> @TableName

BEGIN
insert into @tb values (@LastTableName,@FieldList)
set @FieldList = ''
set @LastTableName = @TableName
END

set @FieldList = case when @FieldList = '' then @ColName else + @FieldList + ',' + @ColName end
fetch curs into @ColName, @TableName
END

deallocate curs

insert into @tb values (@LastTableName,@FieldList)

select * from @tb
.............................................Code END

Pls help
Thanks in Advance


SqlIndia
Post #1400382
Posted Wednesday, December 26, 2012 2:43 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:56 AM
Points: 42,817, Visits: 35,940
sqlindia (12/26/2012)
I have a query that need to Rewrite it without cursor, function or loop to produce same result and should be included in ONE SELECT (no temporary object involved).


Why those restrictions? Sounds like rules for a test or competition...

Look up the FOR XML method of string concatenating.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1400384
Posted Wednesday, December 26, 2012 4:08 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:42 AM
Points: 23,279, Visits: 32,000
Something like this:


WITH CTE AS
(
SELECT
name as TableName,
object_id
FROM sys.tables WHERE schema_id = schema_id('dbo')
)
SELECT TableName,
ColumnList = STUFF((
SELECT ',' + c.name
FROM sys.columns c
WHERE c.object_id = CTE.object_id
ORDER BY c.column_id
FOR XML PATH(''),TYPE).value('.','varchar(max)'),1,1,'')
FROM CTE
ORDER BY TableName;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1400407
Posted Wednesday, December 26, 2012 4:40 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 6:24 PM
Points: 63, Visits: 302
you are right Gila...happened at work and we testing the situation as this raised by someone in group at work.
and I was not able figure after tried. Thought to ask in forum for help.

thanks..


SqlIndia
Post #1400415
Posted Wednesday, December 26, 2012 4:45 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 6:24 PM
Points: 63, Visits: 302
Thanks Lynn...I'll check and share the results..

SqlIndia
Post #1400416
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse