Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Wednesday, March 28, 2012

information inside a function

Hiii

How can i know, when i'm inside a function, what tables are in the select

if my select is:

select myfunction(x)

from table1

I want to know inside myfunction that the table is table1

is there a way of knowing that?

thanks

One way that I can think of to do this is to perform a SET CONTEXT_INFO before calling your scalar UDF and then using the CONTEXT_INFO function from within the context of the UDF. Another potential is to load the information into a permanent table; however, this will require some thought to avoid collisions with other users. This doesn't sound like a real good idea; how are you planning on using this information?

Kent

The better answer / question is why can't you pass this information as an argument to the function?

|||

thanks for the quick answer

The function has to use a select statement which uses the same table and return a value related to it.

this functions will be used by a different user and i dont want the user to type a lot of input parameters.

|||

Hi snorman,

Can you expand what you are trying to accomplish here?

AMB

Wednesday, March 21, 2012

Indexing XML

I have an table-valuated function with XML as parameter. I generate table from that XML. Here is an example of parameter:

<root>
<an>
<anid>5433</anid>
<tix>64</tix>
<dataid>43</dataid>
<datavalue>bc84</datavalue>
</an>
...
</root>

function body begins with:
WITH X(anid, tix, dataid, datavalue, cnt) AS
(
SELECT
new.col.query('./anid').value('.', 'nvarchar(100)') as anid,
new.col.query('./tix').value('.', 'nvarchar(100)') as tix,
new.col.query('./dataid').value('.', 'nvarchar(100)') as dataid,
new.col.query('./datavalue').value('.', 'nvarchar(100)') as datavalue,
cnt = (
SELECT count(*) as cnt
FROM @.xmldata.nodes('/root/an') new(col)
)
FROM
@.xmldata.nodes('/root/an') new(col)
)
....

after what I use X table in joins. The problem is that the X table has no indexes and joins are very slow :[. Is there any possibility to improve performance? That is function and I cannot create index.

thanks a lot

if ur using sqlserver2005 , there is an option for xml index there, though u need to have a primary index defined before u can do it...try it out...

reply if it works out ..

Indexing table-valued function?

I am using a multi-statement table-valued function to assemble data from several tables and views for a report. To do this, I INSERT data into the first few columns and then use UPDATEs to put data additional data into each row. Each UPDATE uses a WHERE criteria that identifies a unique row, based on the value of the first few columns.

The problem I'm having is that the UPDATEs are taking forever to execute. I believe the reason is that the temporary table that's created for the function is not indexed, so each row update requires a complete search of several columns.

In other situations I've been able to define one column as a primary key for the temporary table, but in this situation the primary key would have to consist of four columns, which doesn't seem to be allowed in the table definition for the function.

Is there any way to create indexes for the temporary tables that are created for multistatement table-valued functions? I think that would improve the UPDATE performance dramatically.

Thanks,

Lee Silverman
JackRabbit Sports

-Is it not possible to insert the data with one sql statement that would join all the needed tables together?

-Yes, temporary tables can be indexed just like regular tables.

Code Snippet

create unique clustered idex [IX_TestIndex] on #TmpTable (colA, colB, colC, colD)

Friday, March 9, 2012

Indexes on table variable of table valued function

Hi there,

Can someone tell me if it is possible to add an index to a Table variable that is declare as part of a table valued function ? I've tried the following but I can't get it to work.

ALTER FUNCTION dbo.fnSearch_GetJobsByOccurrence
(
@.param1 int,
@.param2 int
)
RETURNS @.Result TABLE (resultcol1 int, resultcol2 int)
AS
BEGIN

CREATE INDEX resultcol2_ind ON @.Result

-- do some other stuff

RETURN
ENDhttp://support.microsoft.com/default.aspx?scid=kb;en-us;305977&Product=sql2k

Non-clustered indexes cannot be created on table variables, other than the system indexes that are created for a PRIMARY or UNIQUE constraint.|||Thanks for the reply. That link certainly explains it all.

Also I didn't realise that table variables can be stored on disk if MSSQL deems necessary! Taking everything into account I would think temporary tables are somewhat more useful/versatile!?

Cheers.

Sunday, February 19, 2012

Indexed Views SQLServer 2005

I have som problems with Indexed Views.
When I need an isnull or coalesce function in the view, the optimizer
ignores to use it !
Does anyone have a clue? Same code works on SQLServer 2000 !
Regards, Joakim
You can try it out with this code:
Use AdventureWorksDW
go
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
-- You need to change Null options to do the test without isnull funciton,
so let′s prepare that first...
-- alter table factresellersales alter column unitprice money not null
-- alter table factresellersales alter column Orderquantity smallint not null
-- alter table factresellersales alter column DiscountAmount float not null
select TOP 5 ProductKey, SUM(UnitPrice*OrderQuantity)-
SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) Rebate
from dbo.factResellerSales
group by ProductKey
order by Rebate desc
create view Vdiscount1 with schemabinding as
select SUM(isnull(UnitPrice,0)*OrderQuantity) SumPrice,-- isnull in the
view, seems to disqualify it...
SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) SumDiscountPrice,
COUNT_BIG(*) Count, ProductKey
from dbo.factResellerSales
group By ProductKey
go
create unique clustered index VDiscountInd on Vdiscount1 (ProductKey)
Hi Joakim,
this looks like a known issue: SQL Server 2005 might not match indexed views
if the view definition uses ISNULL on a non-nullable column.
You can easily work around the problem: in the view definition, remove
ISNULL for all columns that are not nullable. Note that this does not change
the semantics of your view since the column cannot return NULL values
anyway.
Hope this helps,
-- Torsten
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"Joakim" <Joakim@.discussions.microsoft.com> wrote in message
news:83CECDE6-263A-49E0-925C-C9CF2D6E03D9@.microsoft.com...
>I have som problems with Indexed Views.
> When I need an isnull or coalesce function in the view, the optimizer
> ignores to use it !
> Does anyone have a clue? Same code works on SQLServer 2000 !
> Regards, Joakim
>
> You can try it out with this code:
> Use AdventureWorksDW
> go
> SET NUMERIC_ROUNDABORT OFF;
> SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
> QUOTED_IDENTIFIER, ANSI_NULLS ON;
> GO
> -- You need to change Null options to do the test without isnull funciton,
> so lets prepare that first...
> -- alter table factresellersales alter column unitprice money not null
> -- alter table factresellersales alter column Orderquantity smallint not
> null
> -- alter table factresellersales alter column DiscountAmount float not
> null
> select TOP 5 ProductKey, SUM(UnitPrice*OrderQuantity)-
> SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) Rebate
> from dbo.factResellerSales
> group by ProductKey
> order by Rebate desc
> create view Vdiscount1 with schemabinding as
> select SUM(isnull(UnitPrice,0)*OrderQuantity) SumPrice, -- isnull in the
> view, seems to disqualify it...
> SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) SumDiscountPrice,
> COUNT_BIG(*) Count, ProductKey
> from dbo.factResellerSales
> group By ProductKey
> go
> create unique clustered index VDiscountInd on Vdiscount1 (ProductKey)
>

Indexed Views SQLServer 2005

I have som problems with Indexed Views.
When I need an isnull or coalesce function in the view, the optimizer
ignores to use it !
Does anyone have a clue? Same code works on SQLServer 2000 !
Regards, Joakim
You can try it out with this code:
Use AdventureWorksDW
go
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
-- You need to change Null options to do the test without isnull funciton,
so let′s prepare that first...
-- alter table factresellersales alter column unitprice money not null
-- alter table factresellersales alter column Orderquantity smallint not nul
l
-- alter table factresellersales alter column DiscountAmount float not null
select TOP 5 ProductKey, SUM(UnitPrice*OrderQuantity)-
SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) Rebate
from dbo.factResellerSales
group by ProductKey
order by Rebate desc
create view Vdiscount1 with schemabinding as
select SUM(isnull(UnitPrice,0)*OrderQuantity) SumPrice, -- isnull in the
view, seems to disqualify it...
SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) SumDiscountPrice,
COUNT_BIG(*) Count, ProductKey
from dbo.factResellerSales
group By ProductKey
go
create unique clustered index VDiscountInd on Vdiscount1 (ProductKey)Hi Joakim,
this looks like a known issue: SQL Server 2005 might not match indexed views
if the view definition uses ISNULL on a non-nullable column.
You can easily work around the problem: in the view definition, remove
ISNULL for all columns that are not nullable. Note that this does not change
the semantics of your view since the column cannot return NULL values
anyway.
Hope this helps,
-- Torsten
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"Joakim" <Joakim@.discussions.microsoft.com> wrote in message
news:83CECDE6-263A-49E0-925C-C9CF2D6E03D9@.microsoft.com...
>I have som problems with Indexed Views.
> When I need an isnull or coalesce function in the view, the optimizer
> ignores to use it !
> Does anyone have a clue? Same code works on SQLServer 2000 !
> Regards, Joakim
>
> You can try it out with this code:
> Use AdventureWorksDW
> go
> SET NUMERIC_ROUNDABORT OFF;
> SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
> QUOTED_IDENTIFIER, ANSI_NULLS ON;
> GO
> -- You need to change Null options to do the test without isnull funciton,
> so lets prepare that first...
> -- alter table factresellersales alter column unitprice money not null
> -- alter table factresellersales alter column Orderquantity smallint not
> null
> -- alter table factresellersales alter column DiscountAmount float not
> null
> select TOP 5 ProductKey, SUM(UnitPrice*OrderQuantity)-
> SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) Rebate
> from dbo.factResellerSales
> group by ProductKey
> order by Rebate desc
> create view Vdiscount1 with schemabinding as
> select SUM(isnull(UnitPrice,0)*OrderQuantity) SumPrice, -- isnull in the
> view, seems to disqualify it...
> SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) SumDiscountPrice,
> COUNT_BIG(*) Count, ProductKey
> from dbo.factResellerSales
> group By ProductKey
> go
> create unique clustered index VDiscountInd on Vdiscount1 (ProductKey)
>

Indexed Views SQLServer 2005

I have som problems with Indexed Views.
When I need an isnull or coalesce function in the view, the optimizer
ignores to use it !
Does anyone have a clue? Same code works on SQLServer 2000 !
Regards, Joakim
You can try it out with this code:
Use AdventureWorksDW
go
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
-- You need to change Null options to do the test without isnull funciton,
so let´s prepare that first...
-- alter table factresellersales alter column unitprice money not null
-- alter table factresellersales alter column Orderquantity smallint not null
-- alter table factresellersales alter column DiscountAmount float not null
select TOP 5 ProductKey, SUM(UnitPrice*OrderQuantity)-
SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) Rebate
from dbo.factResellerSales
group by ProductKey
order by Rebate desc
create view Vdiscount1 with schemabinding as
select SUM(isnull(UnitPrice,0)*OrderQuantity) SumPrice, -- isnull in the
view, seems to disqualify it...
SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) SumDiscountPrice,
COUNT_BIG(*) Count, ProductKey
from dbo.factResellerSales
group By ProductKey
go
create unique clustered index VDiscountInd on Vdiscount1 (ProductKey)Hi Joakim,
this looks like a known issue: SQL Server 2005 might not match indexed views
if the view definition uses ISNULL on a non-nullable column.
You can easily work around the problem: in the view definition, remove
ISNULL for all columns that are not nullable. Note that this does not change
the semantics of your view since the column cannot return NULL values
anyway.
Hope this helps,
-- Torsten
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"Joakim" <Joakim@.discussions.microsoft.com> wrote in message
news:83CECDE6-263A-49E0-925C-C9CF2D6E03D9@.microsoft.com...
>I have som problems with Indexed Views.
> When I need an isnull or coalesce function in the view, the optimizer
> ignores to use it !
> Does anyone have a clue? Same code works on SQLServer 2000 !
> Regards, Joakim
>
> You can try it out with this code:
> Use AdventureWorksDW
> go
> SET NUMERIC_ROUNDABORT OFF;
> SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
> QUOTED_IDENTIFIER, ANSI_NULLS ON;
> GO
> -- You need to change Null options to do the test without isnull funciton,
> so let´s prepare that first...
> -- alter table factresellersales alter column unitprice money not null
> -- alter table factresellersales alter column Orderquantity smallint not
> null
> -- alter table factresellersales alter column DiscountAmount float not
> null
> select TOP 5 ProductKey, SUM(UnitPrice*OrderQuantity)-
> SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) Rebate
> from dbo.factResellerSales
> group by ProductKey
> order by Rebate desc
> create view Vdiscount1 with schemabinding as
> select SUM(isnull(UnitPrice,0)*OrderQuantity) SumPrice, -- isnull in the
> view, seems to disqualify it...
> SUM(UnitPrice*OrderQuantity*(1.00-DiscountAmount)) SumDiscountPrice,
> COUNT_BIG(*) Count, ProductKey
> from dbo.factResellerSales
> group By ProductKey
> go
> create unique clustered index VDiscountInd on Vdiscount1 (ProductKey)
>