Showing posts with label defined. Show all posts
Showing posts with label defined. Show all posts

Monday, March 19, 2012

calling a User Defined Function in transactional replication

How do I call a user defined function in a transactional replication. I've
tried a horizontal filter to get rows to replicate but that isn't working
very well.
FYI the makeup of the replication is this: aproximately 20
Publisher/Distributors and 1 central subscriber. All the remote servers use
transactional replication to send the subscriber. The horizontal filter
reduces the number of records but it sometimes does not send all the rows to
the subscriber. Once the repliaction fails to deliver all the rows (usually
after a day or so ) the missing rows are never replicated back.
here is an example - first a udf
CREATE FUNCTION test(@.state char(2))
RETURNS @.retauthors table (au_id char(11))
AS
BEGIN
INSERT INTO @.retauthors
SELECT DISTINCT authors.au_id FROM authors, titleauthor,
(SELECT au_id, test=COUNT(au_id) FROM titleauthor GROUP
BY au_id HAVING COUNT(au_id)>1) AS a
WHERE authors.au_id=titleauthor.au_id
AND a.test>1
AND authors.au_id=a.au_id
AND authors.state=@.state
AND titleauthor.au_id=a.au_id
RETURN
END
SELECT * FROM dbo.authors_filter('CA')
and here is an example of its usage in a filter
WHERE au_id IN (SELECT au_id FROM dbo.Authors_Filter('CA'))
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
"LP" <LP@.discussions.microsoft.com> wrote in message
news:F0027B50-1CF0-4E08-929C-662B60B2CAB2@.microsoft.com...
> How do I call a user defined function in a transactional replication.
I've
> tried a horizontal filter to get rows to replicate but that isn't working
> very well.
>
> FYI the makeup of the replication is this: aproximately 20
> Publisher/Distributors and 1 central subscriber. All the remote servers
use
> transactional replication to send the subscriber. The horizontal filter
> reduces the number of records but it sometimes does not send all the rows
to
> the subscriber. Once the repliaction fails to deliver all the rows
(usually
> after a day or so ) the missing rows are never replicated back.

calling a udf from a computed field

I am trying desparately to build a user defined function on the Microsoft SQL Server 2000 viewer and when it is performed I get the following message:

[Microsoft][ODB SQL Server Driver][SQL Server] Maximum stored procedure, function, trigger, or view nesting level exceeded

Is there any problem using sql MAX() built in function? How can I resolve this issue?

The UDF code is:
CREATE FUNCTION TestFunc (@.Syn int)
RETURNS int AS
BEGIN
IF @.Syn IS NULL
RETURN 1
DECLARE @.NewPersonID INT
SELECT @.NewPersonID = MAX(PersonID)
FROM GO_Test
WHERE SynagogueID = @.Syn
RETURN ISNULL(@.NewPersonID + 1, 1)
END

I call the function from the 'Formula' propery of a computed field of a table like this:
([dbo].[TestFunc]([SynagogueID]))

while SynagogueID is a field on the same table.


The error message indicates that you have reached the maximum nesting level which is 32. But your code doesn't seem to indicate that this error is due to the UDF logic. What operation are you performing when you get the error? You need to trace the SQL statements and find the exact one that is causing the error. Look for nested triggers, UDFs with recursion etc.

Lastly, you should avoid using scalar UDFs especially those that perform data access in computed columns. The performance of queries referencing the computed column will suffer badly. Instead, you can define a view with a sub-select that does the same thing but much more efficiently. Ex:

select ..., coalesce((select max(g.PersonID) from GO_Test AS g where g.SynagogueID = t.SynagogueID), 0) + 1 as PerId

from tbl as t

Sunday, March 11, 2012

Calling a single UDF multiple times within 1 View

Lets say I have a User Defined Function. I need to call the UDF a number of times within the same View. However, the results are not as expected. The only way (I know of) is to have seperate instances on the same function saved under different names. Is there a way to call a single UDF multiple times within a View without the returned values being messed-up?read the sticky at the top of the forum, and why not post what you have.|||Sorry about the ambiguity, missed the new sticky.

I had this problem a few weeks ago, and ended-up having to create multiple instances of the UDF. However, I am now having problems recreating the error and was hoping that someone would have come accross what I described.|||Calling a function multiple times within a view will not screw anything up. There is something else fishy going on. Follow Brett's posting advice.

Wednesday, March 7, 2012

Call remote proc in TSQL

I have two SQL Servers, both in the same domain. My domain account is
defined as a system administrator on both. On one server I set up a linked
server definition to the other and told it to use "logins current security
context". On the second I created a procedure spHelloWorld in master that
creates a table and stuffs a message into it. I tested the proc on that
server and it works fine. I logged into the first server using Query
Analyser using my domain account. I then execute this :
exec RMTSRVRNAME.master.dbo.spHelloWorld.
I get back this error:
Server: Msg 18452, Level 14, State 1, Line 1
Login failed for user '(null)'. Reason: Not associated with a trusted SQL
Server connection.
I f I change the linked server propertyies to always use the sa/password of
the linked server it works fine. What am I missing here? Can't I use
windows authentication with linked servers?
BarryCheck out "Security Account Delegation" in the BOL.
--
Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com
<barryfz@.home.com> wrote in message
news:uk2AGGPvEHA.2536@.TK2MSFTNGP11.phx.gbl...
I have two SQL Servers, both in the same domain. My domain account is
defined as a system administrator on both. On one server I set up a linked
server definition to the other and told it to use "logins current security
context". On the second I created a procedure spHelloWorld in master that
creates a table and stuffs a message into it. I tested the proc on that
server and it works fine. I logged into the first server using Query
Analyser using my domain account. I then execute this :
exec RMTSRVRNAME.master.dbo.spHelloWorld.
I get back this error:
Server: Msg 18452, Level 14, State 1, Line 1
Login failed for user '(null)'. Reason: Not associated with a trusted SQL
Server connection.
I f I change the linked server propertyies to always use the sa/password of
the linked server it works fine. What am I missing here? Can't I use
windows authentication with linked servers?
Barry|||barry
EXEC sp_serveroption SERVER, 'data access' , 'true'
go
use pubs
go
create procedure proc_BARRY
select * from dbo.authors
go
select *
from OPENQUERY(OPUSPENGUIN,'exec pubs.dbo.proc_BARRY')
go
-- clean-up
drop procedure proc_BARRY
go
<barryfz@.home.com> wrote in message
news:uk2AGGPvEHA.2536@.TK2MSFTNGP11.phx.gbl...
> I have two SQL Servers, both in the same domain. My domain account is
> defined as a system administrator on both. On one server I set up a linked
> server definition to the other and told it to use "logins current security
> context". On the second I created a procedure spHelloWorld in master that
> creates a table and stuffs a message into it. I tested the proc on that
> server and it works fine. I logged into the first server using Query
> Analyser using my domain account. I then execute this :
> exec RMTSRVRNAME.master.dbo.spHelloWorld.
> I get back this error:
> Server: Msg 18452, Level 14, State 1, Line 1
> Login failed for user '(null)'. Reason: Not associated with a trusted SQL
> Server connection.
> I f I change the linked server propertyies to always use the sa/password
of
> the linked server it works fine. What am I missing here? Can't I use
> windows authentication with linked servers?
>
> Barry
>|||Hi Barry ,
I wanted to post a quick note to see if you would like additional
assistance or information regarding this particular issue. Have you tried
Uri Dimant's statement? We appreciate your patience and look forward to
hearing from you!
Sincerely yours,
Michael Cheng
Online Partner Support Specialist
Partner Support Group
Microsoft Global Technical Support Center
---
Get Secure! - http://www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Please reply to newsgroups only, many thanks!|||I have check out the recommendations suggested and found them useful but I
am still having a problem. What I have done now is made my remote server a
linked Server using Enterprise Manager and for now I have set the security
to map to the sa for all requests. The remote server is names Mikey. When I
run this:
select * from mikey.master.dbo.CPIRestoreDB
I get back the rows from the table
But when I run this:
exec mikey.master.dbo.spHelloWorld
I get:: Could not execute procedure on remote server 'mikey' because SQL
Server is not configured for remote access.
Why can't I run a sp'
Barry|||barryfz@.home.com wrote:
> I have check out the recommendations suggested and found them useful
> but I am still having a problem. What I have done now is made my
> remote server a linked Server using Enterprise Manager and for now I
> have set the security to map to the sa for all requests. The remote
> server is names Mikey. When I run this:
> select * from mikey.master.dbo.CPIRestoreDB
> I get back the rows from the table
> But when I run this:
> exec mikey.master.dbo.spHelloWorld
> I get:: Could not execute procedure on remote server 'mikey'
> because SQL Server is not configured for remote access.
> Why can't I run a sp'
> Barry
AFAIK, in the linked server properties, check the box "RPC in", right?|||Hi Barry,
Have you tried Zarko Jovanovic's suggestion? I wanted to post a quick note
to see if you would like additional assistance or information regarding
this particular issue. We appreciate your patience and look forward to
hearing from you!
Thank you for your patience and corporation. If you have any questions or
concerns, don't hesitate to let me know. We are always here to be of
assistance!
Sincerely yours,
Michael Cheng
Online Partner Support Specialist
Partner Support Group
Microsoft Global Technical Support Center
---
Get Secure! - http://www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Please reply to newsgroups only, many thanks!

Tuesday, February 14, 2012

Calculating Holidays

Hi all,
I want to calculate holidays for the next 10 years and store them in a
table. Unfortunately a lot of holidays are defined as "the <nth> day of
<Month>." Has anyone done this already, or can you point me to a website
that describes how to do this before I re-invent the wheel?
ThanksI should add this specific technique to http://www.aspfaq.com/2519
Currently, my method determines when the clocks go forward/back, but it
could certainly be enhanced for some others. Easter, however, will be a
very complicated one, and I don't think you can solve it programmatically
for all years. If it is only for 10 years, you might just want to do it
manually...
On 3/12/05 2:19 PM, in article koHYd.14007$Jj4.161@.fe12.lga, "Michael C#"
<xyz@.abcdef.com> wrote:

> Hi all,
> I want to calculate holidays for the next 10 years and store them in a
> table. Unfortunately a lot of holidays are defined as "the <nth> day of
> <Month>." Has anyone done this already, or can you point me to a website
> that describes how to do this before I re-invent the wheel?
> Thanks
>|||http://www.opm.gov/fedhol/
Algorithms: at
http://www.smart.net/~mmontes/ushols.html
International:
http://www.tyzo.com/tools/holidays.html
"Michael C#" wrote:

> Hi all,
> I want to calculate holidays for the next 10 years and store them in a
> table. Unfortunately a lot of holidays are defined as "the <nth> day of
> <Month>." Has anyone done this already, or can you point me to a website
> that describes how to do this before I re-invent the wheel?
> Thanks
>
>|||"Michael C#" <xyz@.abcdef.com> wrote in message
news:koHYd.14007$Jj4.161@.fe12.lga...
> Hi all,
> I want to calculate holidays for the next 10 years and store them in a tab
le.
> Unfortunately a lot of holidays are defined as "the <nth> day of <Month>."
> Has anyone done this already, or can you point me to a website that descri
bes
> how to do this before I re-invent the wheel?
> Thanks
Something as simple as the enumeration of all dates over the next
10 years should be helpful.
CREATE VIEW Digits (d)
AS
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL
SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
SELECT 9
-- Nonnegative integers to some suitable upper bound
CREATE VIEW N (i)
AS
SELECT Ones.d + 10*Tens.d + 100*Hundreds.d + 1000*Thousands.d
FROM Digits AS Ones
CROSS JOIN
Digits AS Tens
CROSS JOIN
Digits AS Hundreds
CROSS JOIN
Digits AS Thousands
CREATE VIEW Dates (d, month, day, year, wday)
AS
SELECT D.d,
MONTH(D.d),
DAY(D.d),
YEAR(D.d),
DATEPART(WEEKDAY, D.d)
FROM (SELECT CAST('20050101' AS DATETIME) + N.i
FROM N) AS D(d)
-- Thanksgiving in the US is the 4th Thursday in November
CREATE VIEW Thanksgiving (d)
AS
SELECT d
FROM Dates AS D1
WHERE month = 11 AND
wday = 5 AND
4 = (SELECT COUNT(*)
FROM Dates AS D2
WHERE month = 11 AND
wday = 5 AND
D1.year = D2.year AND
D2.d <= D1.d)
SELECT d
FROM Thanksgiving
ORDER BY d
-- Memorial Day in the US is the last Monday in May
CREATE VIEW MemorialDay (d)
AS
SELECT d
FROM Dates AS D1
WHERE month = 5 AND
wday = 2 AND
NOT EXISTS (SELECT *
FROM Dates AS D2
WHERE month = 5 AND
wday = 2 AND
D1.year = D2.year
AND
D2.d > D1.d)
SELECT d
FROM MemorialDay
ORDER BY d
JAG|||Michael C# wrote:
> Hi all,
> I want to calculate holidays for the next 10 years and store them in a
> table. Unfortunately a lot of holidays are defined as "the <nth> day of
> <Month>." Has anyone done this already, or can you point me to a website
> that describes how to do this before I re-invent the wheel?
There was a dicussion in the comp.databases.ms-access newsgroup about
this in Oct, 2004. One poster came up w/ some VBA functions that he
claims gets the date for the "X day in the N month." Here is the thread
in Google
85bd8a805473" target="_blank">http://groups-beta.google.com/group...>
85bd8a805473
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)|||Thank you, I was able to adapt the formula at the second link to calculate
the holidays like Labor Day that are defined as the "First Monday in
September", etc. Now I just need to modify to determine Memorial Day,
defined as the "Last Monday in May", and any others like it.
Thanks
"CBretana" <cbretana@.areteIndNOSPAM.com> wrote in message
news:D4CA4A86-3324-4549-AF0B-A2BCD27DC02B@.microsoft.com...
> http://www.opm.gov/fedhol/
> Algorithms: at
> http://www.smart.net/~mmontes/ushols.html
> International:
> http://www.tyzo.com/tools/holidays.html
> "Michael C#" wrote:
>|||Thanks, I actually found a formula I was able to modify to get this value.
Thanks!
"MGFoster" <me@.privacy.com> wrote in message
news:O1KYd.8677$cN6.2566@.newsread1.news.pas.earthlink.net...
> Michael C# wrote:
> There was a dicussion in the comp.databases.ms-access newsgroup about this
> in Oct, 2004. One poster came up w/ some VBA functions that he claims
> gets the date for the "X day in the N month." Here is the thread in Google
> 7285bd8a805473" target="_blank">http://groups-beta.google.com/group...
7285bd8a805473
> --
> MGFoster:::mgf00 <at> earthlink <decimal-point> net
> Oakland, CA (USA)|||Thanks, I've actually found a solution, but I'm going to test yours as well.
It looks very interesting! Thanks
"John Gilson" <jagREMOVE@.acmMUNGE.org> wrote in message
news:UNIYd.32150$534.5183@.twister.nyc.rr.com...
> "Michael C#" <xyz@.abcdef.com> wrote in message
> news:koHYd.14007$Jj4.161@.fe12.lga...
> Something as simple as the enumeration of all dates over the next
> 10 years should be helpful.
> CREATE VIEW Digits (d)
> AS
> SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL
> SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
> SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
> SELECT 9
> -- Nonnegative integers to some suitable upper bound
> CREATE VIEW N (i)
> AS
> SELECT Ones.d + 10*Tens.d + 100*Hundreds.d + 1000*Thousands.d
> FROM Digits AS Ones
> CROSS JOIN
> Digits AS Tens
> CROSS JOIN
> Digits AS Hundreds
> CROSS JOIN
> Digits AS Thousands
> CREATE VIEW Dates (d, month, day, year, wday)
> AS
> SELECT D.d,
> MONTH(D.d),
> DAY(D.d),
> YEAR(D.d),
> DATEPART(WEEKDAY, D.d)
> FROM (SELECT CAST('20050101' AS DATETIME) + N.i
> FROM N) AS D(d)
> -- Thanksgiving in the US is the 4th Thursday in November
> CREATE VIEW Thanksgiving (d)
> AS
> SELECT d
> FROM Dates AS D1
> WHERE month = 11 AND
> wday = 5 AND
> 4 = (SELECT COUNT(*)
> FROM Dates AS D2
> WHERE month = 11 AND
> wday = 5 AND
> D1.year = D2.year AND
> D2.d <= D1.d)
> SELECT d
> FROM Thanksgiving
> ORDER BY d
> -- Memorial Day in the US is the last Monday in May
> CREATE VIEW MemorialDay (d)
> AS
> SELECT d
> FROM Dates AS D1
> WHERE month = 5 AND
> wday = 2 AND
> NOT EXISTS (SELECT *
> FROM Dates AS D2
> WHERE month = 5 AND
> wday = 2
> AND
> D1.year =
> D2.year AND
> D2.d > D1.d)
> SELECT d
> FROM MemorialDay
> ORDER BY d
> --
> JAG
>|||Here's an oblivious version (only sketchily tested) that works for all
years from 1761 forward.
-- Finds the @.n-th @.wd-day in month,year @.m,@.y
declare @. char(168)
set @. = -- required data
'012394594501233450128128345050128348'+
'345012128345045012832834501012834534'+
'501282834501'
-- example: 2-nd Monday in March, 2005
declare @.m int set @.m = 3
declare @.wd int set @.wd = 1 -- Monday = 1, not dependent on datefirst
declare @.n int set @.n = 2
declare @.y int set @.y = 2005
select @.wd+7*@.m-7,substring(@.,@.wd+7*@.m-7,1),
str(1753+substring(@.,@.wd+7*@.m-7,1))+right(100+@.m,2)+'01',
dateadd(
d,
datediff(
d,
str(1753+substring(@.,@.wd+7*@.m-7,1))+right(100+@.m,2)+'01',
str(@.y)+right(100+@.m,2)+'01')/7*7+7*@.n,
str(1753+substring(@.,@.wd+7*@.m-7,1))+right(100+@.m,2)+'01')
Steve Kass
Drew University
MGFoster wrote:

> Michael C# wrote:
>
>
> There was a dicussion in the comp.databases.ms-access newsgroup about
> this in Oct, 2004. One poster came up w/ some VBA functions that he
> claims gets the date for the "X day in the N month." Here is the
> thread in Google
> 7285bd8a805473" target="_blank">http://groups-beta.google.com/group...
7285bd8a805473
>|||I have no idea what this is, but I'm curiously aroused LOFL. I'm definitely
going to take a look at this one.
"Steve Kass" <skass@.drew.edu> wrote in message
news:etafDcGKFHA.580@.TK2MSFTNGP15.phx.gbl...
> Here's an oblivious version (only sketchily tested) that works for all
> years from 1761 forward.
> -- Finds the @.n-th @.wd-day in month,year @.m,@.y
> declare @. char(168)
> set @. = -- required data
> '012394594501233450128128345050128348'+
> '345012128345045012832834501012834534'+
> '501282834501'
> -- example: 2-nd Monday in March, 2005
> declare @.m int set @.m = 3
> declare @.wd int set @.wd = 1 -- Monday = 1, not dependent on datefirst
> declare @.n int set @.n = 2
> declare @.y int set @.y = 2005
> select @.wd+7*@.m-7,substring(@.,@.wd+7*@.m-7,1),
> str(1753+substring(@.,@.wd+7*@.m-7,1))+right(100+@.m,2)+'01',
> dateadd(
> d,
> datediff(
> d,
> str(1753+substring(@.,@.wd+7*@.m-7,1))+right(100+@.m,2)+'01',
> str(@.y)+right(100+@.m,2)+'01')/7*7+7*@.n,
> str(1753+substring(@.,@.wd+7*@.m-7,1))+right(100+@.m,2)+'01')
> Steve Kass
> Drew University
> MGFoster wrote:
>