Solved

how we put this query together

Posted on 2013-01-29
5
281 Views
Last Modified: 2013-02-22
Hi guys, i have a user table,

id    user    
1      bob
2      ted
3      robert

 and this funcion dbo].[Fn_Get] that returns one row of 3 collumns, like this based on the userid.
so if i call the funcion like this : dbo].Fn_Get(1) should return

idl     idb     idc
3        4        5

i want the select to return the users like this.

id    user          idl     idb      idc
1      bob           3        4         5
2      ted            6         3         8
3      robert        4        6        5

so the select query should look like?

select t1.id, t1. from tbuser as t1
0
Comment
Question by:rafaelrgl
5 Comments
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Not real sure of your question. Are you looking for something like this -
select id, user, dbo.Fn_Get(id) from users
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
I think you need { cross | outer } apply, but not 100% sure.


SELECT
    ut.id, ut.user, fg.idl, fg.idb, fg.idc
FROM dbo.userTable ut
CROSS APPLY dbo.Fn_Get(ut.id) AS fg --Fn_Get is a table-valued function


On SQL 2008, you have some additional options using VALUES() also.
0
 
LVL 1

Author Comment

by:rafaelrgl
Comment Utility
yes, it's cross aplly, but can we change the function to a procedure. because i can't use newid() on funcion. So can we make the same query you show me but instead of using a function it will be an procedure? can we?
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
Comment Utility
No, you can't use a procedure that way.

Why would you need NEWID() in a function?
0
 
LVL 10

Assisted Solution

by:deviprasadg
deviprasadg earned 250 total points
Comment Utility
In that case add NEWID to the select statement.(select statement with usertable and crossapply on function)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now