January 22, 2019 at 1:40 am
Excellent article, very thorough. Thanks for sharing this.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 22, 2019 at 10:33 am
Thanks for the great article! Very through and easy understand!
Can someone change my rate to 5 stars instead of one, not sure how to use the rate star, when I clicked the first star without having time to click next, it jumped to the end, so it ended one star.
January 22, 2019 at 11:13 am
Thank you <a title="Go to ChrisM@Works.
January 22, 2019 at 11:15 am
Glad you found it easy to understand sqlfriends. Thank you.
January 22, 2019 at 12:31 pm
Thanks for this nice write-up Kimani, good job!
😎
January 22, 2019 at 12:44 pm
Thanks Eirikur. Hope you found it useful.
January 22, 2019 at 8:12 pm
Agreed with the others. Nice job on the article, Kimani .
BTW/FYI... you CAN actually call a stored procedure from within a function using OPENROWSET. Because of OPENROWSET (it won't take variables) and the inability to use dynamic SQL in a function (although I'll bte someone even figured a way past that), it has some fairly limited uses but for those uses it works a treat.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 23, 2019 at 7:36 am
Thank you Jeff and also, thank you for pointing out that you can certainly call a stored procedure within a function.
I ran the following script on my Dev machine and it certainly worked a treat.
USE StackOverflow2010;
GO
sp_configure 'show advanced options',1
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries',1
RECONFIGURE
GO
CREATE OR ALTER PROCEDURE dbo.NoddyProc
AS
BEGIN
SELECT 1 AS Num;
END
GO
CREATE OR ALTER FUNCTION dbo.itvfnNoddyFunction ()
RETURNS TABLE
AS RETURN (
SELECT np.Num
FROM OPENROWSET('SQLOLEDB','Server=(local);Initial catalog=StackOverflow2010;Trusted_Connection=yes;',
'EXECUTE dbo.NoddyProc') AS np
);
GO
SELECT Num
FROM dbo.itvfnNoddyFunction();
GO
January 23, 2019 at 8:00 am
kimani_m - Wednesday, January 23, 2019 7:36 AMThank you Jeff and also, thank you for pointing out that you can certainly call a stored procedure within a function.
I ran the following script on my Dev machine and it certainly worked a treat.USE StackOverflow2010;
GO
sp_configure 'show advanced options',1
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries',1
RECONFIGURE
GO
CREATE OR ALTER PROCEDURE dbo.NoddyProc
AS
BEGIN
SELECT 1 AS Num;
END
GO
CREATE OR ALTER FUNCTION dbo.itvfnNoddyFunction ()
RETURNS TABLE
AS RETURN (
SELECT np.Num
FROM OPENROWSET('SQLOLEDB','Server=(local);Initial catalog=StackOverflow2010;Trusted_Connection=yes;',
'EXECUTE dbo.NoddyProc') AS np
);
GO
SELECT Num
FROM dbo.itvfnNoddyFunction();
GO
Well Done!
--Jeff Moden
Change is inevitable... Change for the better is not.
June 19, 2020 at 5:38 am
Is the definition of schemabinding not backwards? i.e. Surely it prevents changes to the underlying objects and not the function?
June 19, 2020 at 8:42 am
Hi David. Thanks for pointing that out and as you mentioned, schemabinding will prevent changes to the underlying objects, rather than the function. A better BOL definition can be found here (under schemabinding), rather than the previously referenced one.
I've attached a script(itvf_schemabinding) in case someone would like to observe that behaviour.
Cheers. Kimani
June 19, 2020 at 8:46 am
DROP TABLE IF EXISTS dbo.please_remove;
GO
CREATE TABLE dbo.please_remove (
first_col int NOT NULL
);
GO
INSERT INTO dbo.please_remove
(first_col)
VALUES
(1);
GO
CREATE OR ALTER FUNCTION [dbo].[itv_fn_please_remove] (@id int)
RETURNS TABLE
WITH SCHEMABINDING AS
RETURN (
SELECT @id AS id
FROM dbo.please_remove
);
GO
--1. Function returns expected result e.g. 1
SELECT Id
FROM [dbo].[itv_fn_please_remove] (1);
GO
--2. Dropping underlying objects fails due to schema binding reference
DROP TABLE dbo.please_remove;
GO
--3. Dropping underlying objects succeeded only after dropping binding i.e. dropping the function first
--DROP FUNCTION dbo.itv_fn_please_remove;
--GO
--DROP TABLE dbo.please_remove;
--GO
September 8, 2020 at 8:11 pm
Comments posted to this topic are about the item Creating and Using Inline Table-Valued Functions
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply