April 26, 2017 at 9:54 am
Hello,
This is the first time I am attempting to create a function. I want to call it from my VB.Net application to load a datagrid. The columns are dynamic, so I cannot name the columns because I don't know how many will be returned. My query works without the "Create Function". I also looking into creating a view, but you cannot use "DECLARE" in a view.
Any help is appreciated... thank you!
Here is my statement::
CREATE FUNCTION dbo.PartnerTaxAllocation (@prop AS NVARCHAR(8), @taxproj AS NVARCHAR(8)) RETURNS @T TABLE
DECLARE @cols AS NVARCHAR(MAX),
@query as NVARCHAR(MAX)
Select @cols = STUFF((SELECT ',' + QUOTENAME(name)
from partner p, Tax_Owner_Allocations t
where p.ocode = t.OCode
and t.Entity_Num = '' + @prop + ''
and t.Projection_Year = '' + @taxproj + ''
group by p.name,p.OCode
order by p.OCode
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET @query = 'SELECT allocname, ' + @cols + '
FROM (
SELECT ta.Allocation_Name as [AllocName],
t.Percent_Type as [PercentCode],
a.percent_name as [PercentName],
ta.Allocation_Code as [AllocCode],
p.name as [PartnerName]
from partner p,
Ownership o LEFT OUTER JOIN Tax_Owner_Allocations t on o.ocode = t.ocode
LEFT OUTER JOIN Tax_Allocation ta on t.Allocation_Code = ta.Allocation_Code
LEFT OUTER JOIN Tax_Allocation_Percent a on t.Percent_Type = a.percent_ID
and o.Entity_Num = ''' + @prop + ''' and o.Tax_Projection = ''' + @taxproj + '''
where p.ocode = o.ocode
group by ta.Allocation_Name, ta.allocation_code, p.name,percent_type, percent_name
) as s
PIVOT
(
SUM(Percentcode)
FOR [PartnerName] in (' + @cols + ')
) as pvt '
END
RETURN execute(@query + ' order by pvt.alloccode');
April 26, 2017 at 11:46 am
That situation looks to have contradicting requirements, functions need defined return types but you stated you cannot define the return type ahead of time. Why not try just using the query or maybe a stored procedure instead?
April 26, 2017 at 12:14 pm
functions cannot use dynamic queries; you'll have to use a procedure instead.
Lowell
April 26, 2017 at 12:45 pm
Once the stored procedure is created, how do you call it?
USE [hp_accting]
GO
/****** Object: StoredProcedure [dbo].[PROCPartnerTaxAllocation] Script Date: 4/26/2017 1:14:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[PROCPartnerTaxAllocation] (@prop AS NVARCHAR(8), @taxproj AS NVARCHAR(8))
with exec as caller
as
DECLARE @cols AS NVARCHAR(MAX),
@query as NVARCHAR(MAX)
Select @cols = STUFF((SELECT ',' + QUOTENAME(name)
from partner p, Tax_Owner_Allocations t
where p.ocode = t.OCode
and t.Entity_Num = '' + @prop + ''
and t.Projection_Year = '' + @taxproj + ''
group by p.name,p.OCode
order by p.OCode
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET @query = 'SELECT allocname, ' + @cols + '
FROM (
SELECT ta.Allocation_Name as [AllocName],
t.Percent_Type as [PercentCode],
a.percent_name as [PercentName],
ta.Allocation_Code as [AllocCode],
p.name as [PartnerName]
from partner p,
Ownership o LEFT OUTER JOIN Tax_Owner_Allocations t on o.ocode = t.ocode
LEFT OUTER JOIN Tax_Allocation ta on t.Allocation_Code = ta.Allocation_Code
LEFT OUTER JOIN Tax_Allocation_Percent a on t.Percent_Type = a.percent_ID
and o.Entity_Num = ''' + @prop + ''' and o.Tax_Projection = ''' + @taxproj + '''
where p.ocode = o.ocode
group by ta.Allocation_Name, ta.allocation_code, p.name,percent_type, percent_name
) as s
PIVOT
(
SUM(Percentcode)
FOR [PartnerName] in (' + @cols + ')
) as pvt '
exec(@query + ' order by pvt.alloccode');
Shows it is returning an integer... how do I get it to return a table?
April 26, 2017 at 1:06 pm
should be a result set. work on calling it from your vb.net and examine the result set. It should work just the same as if you had just sent query text from vb.net. try it with a datareader maybe?
April 26, 2017 at 1:37 pm
chof - Wednesday, April 26, 2017 12:45 PMShows it is returning an integer... how do I get it to return a table?
Dim dr As SqlDataReader
Dim src As New BindingSource
Using conn As New SqlConnection(Connection_String)April 26, 2017 at 2:43 pm
Thanks Chris, that did the trick!!
Thank you so much everyone!
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply