| Author |
Topic |
|
nice123ej
Starting Member
48 Posts |
Posted - 2007-07-08 : 03:03:14
|
| HiWhen ever i exec a stored procedure inside a function and then select that function i get this error Only functions and extended stored procedures can be executed from within a function.example _________________________________________________CREATE PROCEDURE [dbo].[prc_test] ASBEGIN SELECT 'Column' , 1END_________________________________________________CREATE FUNCTION dbo.fnc_test() RETURNS int AS BEGIN EXEC prc_test return 1 END_________________________________________________SELECT dbo.fnc_test()when i run this line i get this errorMsg 557, Level 16, State 2, Line 1Only functions and extended stored procedures can be executed from within a function._______________________________________________________How to fix it, turn around it, any ideas?!!it has to be Stored Procedure and Function |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-08 : 03:14:52
|
You can't SQL Server does not allow calling of stored procedure from function. What are you trying to do with the function ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
nice123ej
Starting Member
48 Posts |
Posted - 2007-07-08 : 07:03:24
|
| I have 2 tables Products (ProdID int, ProdQty int, ProdEquation varchar(8000))Customers (CustID int, CustCityID int, CustBusinessID int, CustTarget int)Products.ProdEquation stores an SQL Statements e.g.________________________________________________________________________________________________________________________1001 500 'select count(*) from customers where CustCityID in (17,25,28) and CustTarget>77'1002 750 'select count(*) from customers where CustCityID not in (31,55) and CustBusinessID=4'1003 280 'select count(*) from customers where CustTarget >499'________________________________________________________________________________________________________________________I want to find all products that has execution of its ProdEquation returns 1 customer or moreso i was thinking of something like thisSELECT ProdID FROM Products WHERE FNC_TOTALCUST(ProdEquation) > 0hope someone can help me with thisthanks1 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-08 : 11:49:44
|
| 1) You must turn the function into a stored procedure2) You must use dynamic SQLPeter LarssonHelsingborg, Sweden |
 |
|
|
nice123ej
Starting Member
48 Posts |
Posted - 2007-07-08 : 19:51:54
|
| will you help me to do it?!how to use SP in the where statement?! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-08 : 21:55:34
|
quote: Originally posted by nice123ej will you help me to do it?!how to use SP in the where statement?!
What Peter said is don't use Function at all. Write it as a Stored Procedure KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
nice123ej
Starting Member
48 Posts |
Posted - 2007-07-08 : 22:32:16
|
| how to use SP in the where statement?if i wrote this create procedure prc_TOTALCUST@SQLString varchar(4000)as beginexec(@SQLString )endthen i will not be able to use it in the select statementi.e. I cant do thisSELECT ProdID FROM Products WHERE (EXEC prc_TOTALCUST ProdEquation) > 0any help?! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-09 : 00:34:00
|
quote: how to use SP in the where statement?
You can't. You better read this before going into Dynamic SQL path. http://www.sommarskog.se/dynamic_sql.html KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
nice123ej
Starting Member
48 Posts |
Posted - 2007-07-09 : 02:20:35
|
| Good Article, but still did not find the answer :(Anyone has an idea how to do it?! |
 |
|
|
|