473,434 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Change SP to Function

Hi Forum,
I have an SP that uses a table to retrive data from many tables.


SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
Alter Procedure [dbo].[Fetch_Table_Data]
AS
Begin
-- Declare the variables to store the values returned by FETCH.
DECLARE @Table varchar(8000);
declare @cmd varchar (8000);
set @cmd = ''
DECLARE Table_cursor CURSOR FOR
SELECT distinct ProjektTabelle
FROM dbo.T_TProjekte;

OPEN Table_cursor;
-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.
FETCH NEXT FROM Table_cursor
INTO @Table;

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- Concatenate and display the current values in the variables.
if @cmd <> ''
set @cmd = @cmd + ' union '
set @cmd = @cmd + 'Select CNI,CPROJEKT from ' + @Table
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM Table_cursor
INTO @Table
exec(@cmd)
END
CLOSE Table_cursor
DEALLOCATE Table_cursor
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
Ho would I go about changing this SP to a Function that RETURNS A list of all Records?
thanks in advance
Jeff
Sep 23 '10 #1
2 1203
gpl
152 100+
You will not be able to do this as your code uses an Exec call, because this could return anything, a function will not use it
Sep 23 '10 #2
ck9663
2,878 Expert 2GB
Looks like your exec is being executed multiple times...or am I reading it wrong? If your EXEC is executed only once after building the dynamic query, there's still a solution to your problem. If your EXEC is executed multiple times, I don't know if there's a solution for that.

From the looks of it, you're trying to read the table if the table name are in T_TProjekte table. How many records are there in T_TProjekte?

~~ CK
Sep 23 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
4
by: Daniel | last post by:
Hi, What does the keyword const do for this function? virtual bool isUndoable() const { return true;} Daniel
1
by: Lazlo Woodbine | last post by:
I'm trying to implement a dynamic menu using CSS/DHTML/JavaScript. The menu bar is implemented as hyperlinks so I can use the :hover :active etc. pseudo-styles. When moving from one item to...
11
by: Wayne Cressman | last post by:
I'm writing a function to dynamically change a form validation script depending upon the user's choices. The form onsubmit is: onsubmit="writevalidate(this.select.value);return...
10
by: pozz | last post by:
Hi all, I need to write a simple incrementing/decrementing function like this: unsigned char change( unsigned char x, unsigned char min, unsigned char max, signed char d); x is the value...
1
by: aris1234 | last post by:
Hi All.... i have code html like this : <form> Name : <input type="text"> age : <input type="text"> <input type = "submit"> </form>
1
by: safuk2233 | last post by:
I need a function derived from: javascript: myLightWindow.activateWindow({href: \''. DIR_WS_IMAGES. $product_info .'\', title: \'Waiting for the show to start in Las Vegas\', author:...
2
by: Crazy Mike | last post by:
I have created a change machine and the change function works well. There is a slight problem though as say I am meant to get £1.90 change it gives me 1 pound coin, 3 fifty pence coins and 9...
3
by: Lagon666 | last post by:
How can i change the value of a variant that defined in a function for ever? function test() { this.data = 'hello'; } function change() {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.