[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Select statement with Field Names listed in another table

Posted on 2006-05-31
3
Medium Priority
?
395 Views
Last Modified: 2007-12-19
I have a table that lists a bunch of field names from multiple tables.  I have the names setup with the table.fieldname so that I can draw information Example:
Job.JobID
Customers.CustomerName
Employees.EmployeeName

I also have the from statement built that has all the joins necessary to grab the data from these field names.

This sql statement/stored procedure will also have where and order by statements that will need to be appended to the end of the procedure.

I need to know the best way to do this.

An good and clear example would be great and would be worth 500 pts. to me

Thanks.
0
Comment
Question by:Sheritlw
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16805047
declare @fields table (FiledName varchar(255))
INSERT INTO @fields SELECT 'ID'
INSERT INTO @fields SELECT 'NAME'

declare @op nvarchar(1000)
SELECT @op = COALESCE(LTRIM(@Op)+',','')+FiledName
FROM @fields

EXEC('SELECT '+@op+ ' FROM sysobjects ')
0
 
LVL 11

Accepted Solution

by:
ram2098 earned 2000 total points
ID: 16805137
Just to give an idea ..you may have to do this way....(I've not put the syntax)

Declare @SQL varchar(8000)

select @sql = 'SELECT '

declare cursor ...select fieldcol from fieldtable

--for each row in cursor loop through and add it to the select
  select @sql = @sql + @fieldcol + ','
--until the end of loop

select @sql = @sql + @FromClause

select @sql = @sql + @whereclause

Select @sql = @sql + @orderbyclause

exec(@sql)

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16807532
why do you want to use dymanic sql to query your database...?

Static SQL is usually best from a performance and security viewpoint..!

or are you just trying to generate your Stored Procedures?



0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

831 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