Showing posts with label sample. Show all posts
Showing posts with label sample. Show all posts

Sunday, March 25, 2012

Computed columns in temp tables

I am having a problem with using UDF as part of a temp table computed
column. Here's the sample code:
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
'fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
DECLARE @.z INT
SET @.z = @.x + @.y
RETURN @.z
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (dbo.fn_test(x,y))
)
I receive the following error:
Server: Msg 208, Level 16, State 1, Line 2
Invalid object name 'dbo.fn_test'.
I do not get this error if I use a regular table.
HELP!
Looks like the UDF is being looked up in the tempdb database. If you created
the UDF in tempdb, your table creation will succeed. I am not sure if this
is the expected behavior. I'll post again, if I find out more.
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
I am having a problem with using UDF as part of a temp table computed
column. Here's the sample code:
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
'fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
DECLARE @.z INT
SET @.z = @.x + @.y
RETURN @.z
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (dbo.fn_test(x,y))
)
I receive the following error:
Server: Msg 208, Level 16, State 1, Line 2
Invalid object name 'dbo.fn_test'.
I do not get this error if I use a regular table.
HELP!
|||Hi,
As far as I know, You cannot use a UDF inside the table creation. It should
be
create table #x(i int,j int,k as i+j)
insert into #x(i,j) values(1,10)
select * from #x
output will be:-
i j k
-- -- --
1 10 11
(1 row(s) affected)
Thanks
Hari
MCDBA
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
> I am having a problem with using UDF as part of a temp table computed
> column. Here's the sample code:
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
> 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> DECLARE @.z INT
> SET @.z = @.x + @.y
> RETURN @.z
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (dbo.fn_test(x,y))
> )
> I receive the following error:
> Server: Msg 208, Level 16, State 1, Line 2
> Invalid object name 'dbo.fn_test'.
> I do not get this error if I use a regular table.
> HELP!
>
|||Sorry for the wrong information.
Thanks Vyas. I have never tried this option.
Thanks
Hari
MCDBA
"Hari" <hari_prasad_k@.hotmail.com> wrote in message
news:OLTxVgqWEHA.716@.TK2MSFTNGP11.phx.gbl...
> Hi,
> As far as I know, You cannot use a UDF inside the table creation. It
should[vbcol=seagreen]
> be
> create table #x(i int,j int,k as i+j)
> insert into #x(i,j) values(1,10)
> select * from #x
> output will be:-
> i j k
> -- -- --
> 1 10 11
> (1 row(s) affected)
>
> --
> Thanks
> Hari
> MCDBA
> "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
=
>
|||I have thought that was the case but no luck! I modified the code to
explicitly call the function in the database where it was created and still
got the same error. Here's modified code:
use master
GO
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
'fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
RETURN @.x + @.y
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (master.dbo.fn_test(x,y))
)
"Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
news:O52L6fqWEHA.2544@.TK2MSFTNGP10.phx.gbl...
> Looks like the UDF is being looked up in the tempdb database. If you
created
> the UDF in tempdb, your table creation will succeed. I am not sure if this
> is the expected behavior. I'll post again, if I find out more.
> --
> HTH,
> Vyas, MVP (SQL Server)
> http://vyaskn.tripod.com/
> Is .NET important for a database professional?
> http://vyaskn.tripod.com/poll.htm
>
> "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
> I am having a problem with using UDF as part of a temp table computed
> column. Here's the sample code:
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
> 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> DECLARE @.z INT
> SET @.z = @.x + @.y
> RETURN @.z
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (dbo.fn_test(x,y))
> )
> I receive the following error:
> Server: Msg 208, Level 16, State 1, Line 2
> Invalid object name 'dbo.fn_test'.
> I do not get this error if I use a regular table.
> HELP!
>
>
|||As Vyas pointed out, the issue is that you have to create the function in
tempdb. I think he is right in suggesting that you can't use UDFs that
reside in a different database in the definition of a table.
Jacco Schalkwijk
SQL Server MVP
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:%23siqQFrWEHA.3512@.TK2MSFTNGP12.phx.gbl...
> I have thought that was the case but no luck! I modified the code to
> explicitly call the function in the database where it was created and
still[vbcol=seagreen]
> got the same error. Here's modified code:
> use master
> GO
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
> 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> RETURN @.x + @.y
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (master.dbo.fn_test(x,y))
> )
>
> "Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
> news:O52L6fqWEHA.2544@.TK2MSFTNGP10.phx.gbl...
> created
this[vbcol=seagreen]
=
>
|||Hi,
temporary tables are always made in tempdb (and only tempdb for your solution)
change
use master
on
use tempdb
Regards,
Pablo
"Steven Yampolsky" wrote:

> I have thought that was the case but no luck! I modified the code to
> explicitly call the function in the database where it was created and still
> got the same error. Here's modified code:
> use master
> GO
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
> 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> RETURN @.x + @.y
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (master.dbo.fn_test(x,y))
> )
>
> "Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
> news:O52L6fqWEHA.2544@.TK2MSFTNGP10.phx.gbl...
> created
>
>
|||How can I create a function in tempdb while inside a stored procedure? I
don't think I can do that.
Lets assume I can use sp_executesql to create the function inside a SP. What
will be a lifespan of it? Will it get dropped once the session is closed?
will it be accessible from other sessions? Will there be a name conflict
when two sessions will try and execute the same SP?
The more I get into it, the more questions I get. Hopefully I'll come out
with good knowledge!
Steve
"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid > wrote
in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
> As Vyas pointed out, the issue is that you have to create the function in
> tempdb. I think he is right in suggesting that you can't use UDFs that
> reside in a different database in the definition of a table.
>
|||How can I create a function in tempdb while inside a stored procedure? I
don't think I can do that.
Lets assume I can use sp_executesql to create the function inside a SP. What
will be a lifespan of it? Will it get dropped once the session is closed?
will it be accessible from other sessions? Will there be a name conflict
when two sessions will try and execute the same SP?
The more I get into it, the more questions I get. Hopefully I'll come out
with good knowledge!
Steve
"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid > wrote
in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
> As Vyas pointed out, the issue is that you have to create the function in
> tempdb. I think he is right in suggesting that you can't use UDFs that
> reside in a different database in the definition of a table.
>
|||Create a permanent function in tempdb, like
USE tempdb
GO
CREATE FUNCTION dbo.fnTest
(@.x int, @.y int)
RETURNS int
AS
BEGIN
RETURN (@.x+@.y)
END
GO
Now using another database u can call this function during temp table
creation
eg.
USE Pubs
CREATE TABLE #temp(x int,y int, z as dbo.fnTest(x,y))
INSERT INTO #temp VALUES(1,2)
INSERT INTO #temp VALUES(3,2)
SELECT * FROM #temp
Roji. P. Thomas
SQL Server Programmer
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eLwmCwRXEHA.2940@.TK2MSFTNGP09.phx.gbl...
> How can I create a function in tempdb while inside a stored procedure? I
> don't think I can do that.
> Lets assume I can use sp_executesql to create the function inside a SP.
What
> will be a lifespan of it? Will it get dropped once the session is closed?
> will it be accessible from other sessions? Will there be a name conflict
> when two sessions will try and execute the same SP?
> The more I get into it, the more questions I get. Hopefully I'll come out
> with good knowledge!
> Steve
> "Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid >
wrote[vbcol=seagreen]
> in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
in
>

Computed columns in temp tables

I am having a problem with using UDF as part of a temp table computed
column. Here's the sample code:
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
'fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
DECLARE @.z INT
SET @.z = @.x + @.y
RETURN @.z
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (dbo.fn_test(x,y))
)
I receive the following error:
Server: Msg 208, Level 16, State 1, Line 2
Invalid object name 'dbo.fn_test'.
I do not get this error if I use a regular table.
HELP!Looks like the UDF is being looked up in the tempdb database. If you created
the UDF in tempdb, your table creation will succeed. I am not sure if this
is the expected behavior. I'll post again, if I find out more.
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
I am having a problem with using UDF as part of a temp table computed
column. Here's the sample code:
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
'fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
DECLARE @.z INT
SET @.z = @.x + @.y
RETURN @.z
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (dbo.fn_test(x,y))
)
I receive the following error:
Server: Msg 208, Level 16, State 1, Line 2
Invalid object name 'dbo.fn_test'.
I do not get this error if I use a regular table.
HELP!|||Hi,
As far as I know, You cannot use a UDF inside the table creation. It should
be
create table #x(i int,j int,k as i+j)
insert into #x(i,j) values(1,10)
select * from #x
output will be:-
i j k
-- -- --
1 10 11
(1 row(s) affected)
Thanks
Hari
MCDBA
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
> I am having a problem with using UDF as part of a temp table computed
> column. Here's the sample code:
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
> 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> DECLARE @.z INT
> SET @.z = @.x + @.y
> RETURN @.z
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (dbo.fn_test(x,y))
> )
> I receive the following error:
> Server: Msg 208, Level 16, State 1, Line 2
> Invalid object name 'dbo.fn_test'.
> I do not get this error if I use a regular table.
> HELP!
>|||Sorry for the wrong information.
Thanks Vyas. I have never tried this option.
Thanks
Hari
MCDBA
"Hari" <hari_prasad_k@.hotmail.com> wrote in message
news:OLTxVgqWEHA.716@.TK2MSFTNGP11.phx.gbl...
> Hi,
> As far as I know, You cannot use a UDF inside the table creation. It
should
> be
> create table #x(i int,j int,k as i+j)
> insert into #x(i,j) values(1,10)
> select * from #x
> output will be:-
> i j k
> -- -- --
> 1 10 11
> (1 row(s) affected)
>
> --
> Thanks
> Hari
> MCDBA
> "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
=[vbcol=seagreen]
>|||I have thought that was the case but no luck! I modified the code to
explicitly call the function in the database where it was created and still
got the same error. Here's modified code:
use master
GO
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
'fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
RETURN @.x + @.y
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (master.dbo.fn_test(x,y))
)
"Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
news:O52L6fqWEHA.2544@.TK2MSFTNGP10.phx.gbl...
> Looks like the UDF is being looked up in the tempdb database. If you
created
> the UDF in tempdb, your table creation will succeed. I am not sure if this
> is the expected behavior. I'll post again, if I find out more.
> --
> HTH,
> Vyas, MVP (SQL Server)
> http://vyaskn.tripod.com/
> Is .NET important for a database professional?
> http://vyaskn.tripod.com/poll.htm
>
> "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
> I am having a problem with using UDF as part of a temp table computed
> column. Here's the sample code:
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
> 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> DECLARE @.z INT
> SET @.z = @.x + @.y
> RETURN @.z
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (dbo.fn_test(x,y))
> )
> I receive the following error:
> Server: Msg 208, Level 16, State 1, Line 2
> Invalid object name 'dbo.fn_test'.
> I do not get this error if I use a regular table.
> HELP!
>
>|||As Vyas pointed out, the issue is that you have to create the function in
tempdb. I think he is right in suggesting that you can't use UDFs that
reside in a different database in the definition of a table.
Jacco Schalkwijk
SQL Server MVP
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:%23siqQFrWEHA.3512@.TK2MSFTNGP12.phx.gbl...
> I have thought that was the case but no luck! I modified the code to
> explicitly call the function in the database where it was created and
still
> got the same error. Here's modified code:
> use master
> GO
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
> 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> RETURN @.x + @.y
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (master.dbo.fn_test(x,y))
> )
>
> "Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
> news:O52L6fqWEHA.2544@.TK2MSFTNGP10.phx.gbl...
> created
this[vbcol=seagreen]
=[vbcol=seagreen]
>|||Hi,
temporary tables are always made in tempdb (and only tempdb for your solutio
n)
change
use master
on
use tempdb
Regards,
Pablo
"Steven Yampolsky" wrote:

> I have thought that was the case but no luck! I modified the code to
> explicitly call the function in the database where it was created and stil
l
> got the same error. Here's modified code:
> use master
> GO
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name =
> 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> RETURN @.x + @.y
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (master.dbo.fn_test(x,y))
> )
>
> "Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
> news:O52L6fqWEHA.2544@.TK2MSFTNGP10.phx.gbl...
> created
>
>|||How can I create a function in tempdb while inside a stored procedure? I
don't think I can do that.
Lets assume I can use sp_executesql to create the function inside a SP. What
will be a lifespan of it? Will it get dropped once the session is closed?
will it be accessible from other sessions? Will there be a name conflict
when two sessions will try and execute the same SP?
The more I get into it, the more questions I get. Hopefully I'll come out
with good knowledge!
Steve
"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid> wrote
in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
> As Vyas pointed out, the issue is that you have to create the function in
> tempdb. I think he is right in suggesting that you can't use UDFs that
> reside in a different database in the definition of a table.
>|||Create a permanent function in tempdb, like
USE tempdb
GO
CREATE FUNCTION dbo.fnTest
(@.x int, @.y int)
RETURNS int
AS
BEGIN
RETURN (@.x+@.y)
END
GO
Now using another database u can call this function during temp table
creation
eg.
USE Pubs
CREATE TABLE #temp(x int,y int, z as dbo.fnTest(x,y))
INSERT INTO #temp VALUES(1,2)
INSERT INTO #temp VALUES(3,2)
SELECT * FROM #temp
Roji. P. Thomas
SQL Server Programmer
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eLwmCwRXEHA.2940@.TK2MSFTNGP09.phx.gbl...
> How can I create a function in tempdb while inside a stored procedure? I
> don't think I can do that.
> Lets assume I can use sp_executesql to create the function inside a SP.
What
> will be a lifespan of it? Will it get dropped once the session is closed?
> will it be accessible from other sessions? Will there be a name conflict
> when two sessions will try and execute the same SP?
> The more I get into it, the more questions I get. Hopefully I'll come out
> with good knowledge!
> Steve
> "Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid>
wrote
> in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
in[vbcol=seagreen]
>|||Won't that function get dropped/cleaned up and will require constant
checking for its existance?
Steve
"Roji. P. Thomas" <lazydragon@.nowhere.com> wrote in message
news:e5MzPCSXEHA.3016@.tk2msftngp13.phx.gbl...
> Create a permanent function in tempdb, like
> USE tempdb
> GO
>
> CREATE FUNCTION dbo.fnTest
> (@.x int, @.y int)
> RETURNS int
> AS
> BEGIN
> RETURN (@.x+@.y)
> END
> GO
> Now using another database u can call this function during temp table
> creation
> eg.
>
> USE Pubs
>
> CREATE TABLE #temp(x int,y int, z as dbo.fnTest(x,y))
>
> INSERT INTO #temp VALUES(1,2)
> INSERT INTO #temp VALUES(3,2)
> SELECT * FROM #temp
>
> --
> Roji. P. Thomas
> SQL Server Programmer
> "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> news:eLwmCwRXEHA.2940@.TK2MSFTNGP09.phx.gbl...
> What
closed?[vbcol=seagreen]
out[vbcol=seagreen]
> wrote
> in
>

Computed columns in temp tables

I am having a problem with using UDF as part of a temp table computed column. Here's the sample code:
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name = 'fn_test')
DROP FUNCTION dbo.fn_testGO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
DECLARE @.z INT
SET @.z = @.x + @.y
RETURN @.z
END
GO

CREATE TABLE #X
(
x INT,
y INT,
z AS (dbo.fn_test(x,y))
)
I receive the following error:

Server: Msg 208, Level 16, State 1, Line 2
Invalid object name 'dbo.fn_test'.

I do not get this error if I use a regular table.
HELP!The function you are creating resides in your user database, where as the temporay table is actually created in tempdb database, where your function does not exist.

To test this out I created the function in tempdb and then went into my user database (ex. testdb) and created the temporay table using your script and it worked fine.

The problem with creating a function in tempdb is that the next time SQL Server stops and then restarts the function will be gone. The way around this is to also create the function in model database, since each time SQL Server starts up it takes a copy of model to create tempdb.|||Can you post your script please. I have tried creating the function in the master database and then use it in the temp table creation like this:

z AS (master.dbo.fn_test(x,y))

and still got the error. :confused:|||Let's assume that your user database is named foo for the purpose of this posting. When you create a function named fn_test in that database, the three part name for that function is foo.dbo.fn_test. Within foo, you can refer to it as dbo.fn_test, but in other databases on the server you'll need to use the three part name, or foo.dbo.fn_test.

Since a temp table resides in tempdb, it is in another database (not really in foo at all). Because of that in order for your computed column to work properly, I think that you'll have to use:CREATE TABLE #X
(
x INT,
y INT,
z AS (foo.dbo.fn_test(x,y))
)-PatP|||I did just that and still got the same error. Previous poster mentioned that he was able to do it but haven't posted his version of the script.

Steve

Let's assume that your user database is named foo for the purpose of this posting. When you create a function named fn_test in that database, the three part name for that function is foo.dbo.fn_test. Within foo, you can refer to it as dbo.fn_test, but in other databases on the server you'll need to use the three part name, or foo.dbo.fn_test.

Since a temp table resides in tempdb, it is in another database (not really in foo at all). Because of that in order for your computed column to work properly, I think that you'll have to use:CREATE TABLE #X
(
x INT,
y INT,
z AS (foo.dbo.fn_test(x,y))
)-PatP|||I have a database called Test, where I'm creating the temporay table.

set nocount on
go
use tempdb
go
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name = 'fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
DECLARE @.z INT
SET @.z = @.x + @.y
RETURN @.z
END
GO

use test
go
CREATE TABLE #X
(
x INT,
y INT,
z AS (dbo.fn_test(x,y))
)
go
insert #x values (5,6)
go
select * from #x
go
drop table #xsqlsql

Computed columns in temp tables

I am having a problem with using UDF as part of a temp table computed
column. Here's the sample code:
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name = 'fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
DECLARE @.z INT
SET @.z = @.x + @.y
RETURN @.z
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (dbo.fn_test(x,y))
)
I receive the following error:
Server: Msg 208, Level 16, State 1, Line 2
Invalid object name 'dbo.fn_test'.
I do not get this error if I use a regular table.
HELP!Hi,
As far as I know, You cannot use a UDF inside the table creation. It should
be
create table #x(i int,j int,k as i+j)
insert into #x(i,j) values(1,10)
select * from #x
output will be:-
i j k
-- -- --
1 10 11
(1 row(s) affected)
Thanks
Hari
MCDBA
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
> I am having a problem with using UDF as part of a temp table computed
> column. Here's the sample code:
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name => 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> DECLARE @.z INT
> SET @.z = @.x + @.y
> RETURN @.z
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (dbo.fn_test(x,y))
> )
> I receive the following error:
> Server: Msg 208, Level 16, State 1, Line 2
> Invalid object name 'dbo.fn_test'.
> I do not get this error if I use a regular table.
> HELP!
>|||Looks like the UDF is being looked up in the tempdb database. If you created
the UDF in tempdb, your table creation will succeed. I am not sure if this
is the expected behavior. I'll post again, if I find out more.
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
I am having a problem with using UDF as part of a temp table computed
column. Here's the sample code:
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name ='fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
DECLARE @.z INT
SET @.z = @.x + @.y
RETURN @.z
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (dbo.fn_test(x,y))
)
I receive the following error:
Server: Msg 208, Level 16, State 1, Line 2
Invalid object name 'dbo.fn_test'.
I do not get this error if I use a regular table.
HELP!|||Sorry for the wrong information.
Thanks Vyas. I have never tried this option.
--
Thanks
Hari
MCDBA
"Hari" <hari_prasad_k@.hotmail.com> wrote in message
news:OLTxVgqWEHA.716@.TK2MSFTNGP11.phx.gbl...
> Hi,
> As far as I know, You cannot use a UDF inside the table creation. It
should
> be
> create table #x(i int,j int,k as i+j)
> insert into #x(i,j) values(1,10)
> select * from #x
> output will be:-
> i j k
> -- -- --
> 1 10 11
> (1 row(s) affected)
>
> --
> Thanks
> Hari
> MCDBA
> "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
> > I am having a problem with using UDF as part of a temp table computed
> > column. Here's the sample code:
> >
> > IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name
=> > 'fn_test')
> > DROP FUNCTION dbo.fn_test
> > GO
> >
> > CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> > RETURNS INT AS
> > BEGIN
> > DECLARE @.z INT
> > SET @.z = @.x + @.y
> > RETURN @.z
> > END
> > GO
> >
> > CREATE TABLE #X
> > (
> > x INT,
> > y INT,
> > z AS (dbo.fn_test(x,y))
> > )
> >
> > I receive the following error:
> >
> > Server: Msg 208, Level 16, State 1, Line 2
> > Invalid object name 'dbo.fn_test'.
> >
> > I do not get this error if I use a regular table.
> > HELP!
> >
> >
>|||I have thought that was the case but no luck! I modified the code to
explicitly call the function in the database where it was created and still
got the same error. Here's modified code:
use master
GO
IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name ='fn_test')
DROP FUNCTION dbo.fn_test
GO
CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
RETURNS INT AS
BEGIN
RETURN @.x + @.y
END
GO
CREATE TABLE #X
(
x INT,
y INT,
z AS (master.dbo.fn_test(x,y))
)
"Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
news:O52L6fqWEHA.2544@.TK2MSFTNGP10.phx.gbl...
> Looks like the UDF is being looked up in the tempdb database. If you
created
> the UDF in tempdb, your table creation will succeed. I am not sure if this
> is the expected behavior. I'll post again, if I find out more.
> --
> HTH,
> Vyas, MVP (SQL Server)
> http://vyaskn.tripod.com/
> Is .NET important for a database professional?
> http://vyaskn.tripod.com/poll.htm
>
> "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
> I am having a problem with using UDF as part of a temp table computed
> column. Here's the sample code:
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name => 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> DECLARE @.z INT
> SET @.z = @.x + @.y
> RETURN @.z
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (dbo.fn_test(x,y))
> )
> I receive the following error:
> Server: Msg 208, Level 16, State 1, Line 2
> Invalid object name 'dbo.fn_test'.
> I do not get this error if I use a regular table.
> HELP!
>
>|||As Vyas pointed out, the issue is that you have to create the function in
tempdb. I think he is right in suggesting that you can't use UDFs that
reside in a different database in the definition of a table.
--
Jacco Schalkwijk
SQL Server MVP
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:%23siqQFrWEHA.3512@.TK2MSFTNGP12.phx.gbl...
> I have thought that was the case but no luck! I modified the code to
> explicitly call the function in the database where it was created and
still
> got the same error. Here's modified code:
> use master
> GO
> IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name => 'fn_test')
> DROP FUNCTION dbo.fn_test
> GO
> CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> RETURNS INT AS
> BEGIN
> RETURN @.x + @.y
> END
> GO
> CREATE TABLE #X
> (
> x INT,
> y INT,
> z AS (master.dbo.fn_test(x,y))
> )
>
> "Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
> news:O52L6fqWEHA.2544@.TK2MSFTNGP10.phx.gbl...
> > Looks like the UDF is being looked up in the tempdb database. If you
> created
> > the UDF in tempdb, your table creation will succeed. I am not sure if
this
> > is the expected behavior. I'll post again, if I find out more.
> > --
> > HTH,
> > Vyas, MVP (SQL Server)
> > http://vyaskn.tripod.com/
> > Is .NET important for a database professional?
> > http://vyaskn.tripod.com/poll.htm
> >
> >
> > "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> > news:eZChvYqWEHA.2804@.TK2MSFTNGP10.phx.gbl...
> > I am having a problem with using UDF as part of a temp table computed
> > column. Here's the sample code:
> >
> > IF EXISTS( SELECT 1 FROM information_schema.routines WHERE routine_name
=> > 'fn_test')
> > DROP FUNCTION dbo.fn_test
> > GO
> >
> > CREATE FUNCTION dbo.fn_test( @.x int, @.y int)
> > RETURNS INT AS
> > BEGIN
> > DECLARE @.z INT
> > SET @.z = @.x + @.y
> > RETURN @.z
> > END
> > GO
> >
> > CREATE TABLE #X
> > (
> > x INT,
> > y INT,
> > z AS (dbo.fn_test(x,y))
> > )
> >
> > I receive the following error:
> >
> > Server: Msg 208, Level 16, State 1, Line 2
> > Invalid object name 'dbo.fn_test'.
> >
> > I do not get this error if I use a regular table.
> > HELP!
> >
> >
> >
>|||How can I create a function in tempdb while inside a stored procedure? I
don't think I can do that.
Lets assume I can use sp_executesql to create the function inside a SP. What
will be a lifespan of it? Will it get dropped once the session is closed?
will it be accessible from other sessions? Will there be a name conflict
when two sessions will try and execute the same SP?
The more I get into it, the more questions I get. Hopefully I'll come out
with good knowledge! :)
Steve
"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid> wrote
in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
> As Vyas pointed out, the issue is that you have to create the function in
> tempdb. I think he is right in suggesting that you can't use UDFs that
> reside in a different database in the definition of a table.
>|||Create a permanent function in tempdb, like
USE tempdb
GO
CREATE FUNCTION dbo.fnTest
(@.x int, @.y int)
RETURNS int
AS
BEGIN
RETURN (@.x+@.y)
END
GO
Now using another database u can call this function during temp table
creation
eg.
USE Pubs
CREATE TABLE #temp(x int,y int, z as dbo.fnTest(x,y))
INSERT INTO #temp VALUES(1,2)
INSERT INTO #temp VALUES(3,2)
SELECT * FROM #temp
Roji. P. Thomas
SQL Server Programmer
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:eLwmCwRXEHA.2940@.TK2MSFTNGP09.phx.gbl...
> How can I create a function in tempdb while inside a stored procedure? I
> don't think I can do that.
> Lets assume I can use sp_executesql to create the function inside a SP.
What
> will be a lifespan of it? Will it get dropped once the session is closed?
> will it be accessible from other sessions? Will there be a name conflict
> when two sessions will try and execute the same SP?
> The more I get into it, the more questions I get. Hopefully I'll come out
> with good knowledge! :)
> Steve
> "Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid>
wrote
> in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
> > As Vyas pointed out, the issue is that you have to create the function
in
> > tempdb. I think he is right in suggesting that you can't use UDFs that
> > reside in a different database in the definition of a table.
> >
>|||Won't that function get dropped/cleaned up and will require constant
checking for its existance?
Steve
"Roji. P. Thomas" <lazydragon@.nowhere.com> wrote in message
news:e5MzPCSXEHA.3016@.tk2msftngp13.phx.gbl...
> Create a permanent function in tempdb, like
> USE tempdb
> GO
>
> CREATE FUNCTION dbo.fnTest
> (@.x int, @.y int)
> RETURNS int
> AS
> BEGIN
> RETURN (@.x+@.y)
> END
> GO
> Now using another database u can call this function during temp table
> creation
> eg.
>
> USE Pubs
>
> CREATE TABLE #temp(x int,y int, z as dbo.fnTest(x,y))
>
> INSERT INTO #temp VALUES(1,2)
> INSERT INTO #temp VALUES(3,2)
> SELECT * FROM #temp
>
> --
> Roji. P. Thomas
> SQL Server Programmer
> "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> news:eLwmCwRXEHA.2940@.TK2MSFTNGP09.phx.gbl...
> > How can I create a function in tempdb while inside a stored procedure? I
> > don't think I can do that.
> >
> > Lets assume I can use sp_executesql to create the function inside a SP.
> What
> > will be a lifespan of it? Will it get dropped once the session is
closed?
> > will it be accessible from other sessions? Will there be a name conflict
> > when two sessions will try and execute the same SP?
> >
> > The more I get into it, the more questions I get. Hopefully I'll come
out
> > with good knowledge! :)
> >
> > Steve
> >
> > "Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid>
> wrote
> > in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
> > > As Vyas pointed out, the issue is that you have to create the function
> in
> > > tempdb. I think he is right in suggesting that you can't use UDFs that
> > > reside in a different database in the definition of a table.
> > >
> >
> >
>|||You can either add the function to the model database (on the basis of which
all databases are created) or you can create a start up stored procedure to
create the function in tempdb whenever SQL Server is started. The function
will not just disappear from tempdb once you have created it, only when SQL
Server is restarted, because tempdb gets recreated then.
--
Jacco Schalkwijk
SQL Server MVP
"Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
news:ufdhcNTXEHA.1656@.TK2MSFTNGP09.phx.gbl...
> Won't that function get dropped/cleaned up and will require constant
> checking for its existance?
> Steve
> "Roji. P. Thomas" <lazydragon@.nowhere.com> wrote in message
> news:e5MzPCSXEHA.3016@.tk2msftngp13.phx.gbl...
> > Create a permanent function in tempdb, like
> >
> > USE tempdb
> > GO
> >
> >
> > CREATE FUNCTION dbo.fnTest
> > (@.x int, @.y int)
> > RETURNS int
> > AS
> > BEGIN
> > RETURN (@.x+@.y)
> > END
> > GO
> >
> > Now using another database u can call this function during temp table
> > creation
> >
> > eg.
> >
> >
> > USE Pubs
> >
> >
> > CREATE TABLE #temp(x int,y int, z as dbo.fnTest(x,y))
> >
> >
> > INSERT INTO #temp VALUES(1,2)
> > INSERT INTO #temp VALUES(3,2)
> >
> > SELECT * FROM #temp
> >
> >
> >
> > --
> > Roji. P. Thomas
> > SQL Server Programmer
> > "Steven Yampolsky" <syampolsky@.eagleinvsys.com> wrote in message
> > news:eLwmCwRXEHA.2940@.TK2MSFTNGP09.phx.gbl...
> > > How can I create a function in tempdb while inside a stored procedure?
I
> > > don't think I can do that.
> > >
> > > Lets assume I can use sp_executesql to create the function inside a
SP.
> > What
> > > will be a lifespan of it? Will it get dropped once the session is
> closed?
> > > will it be accessible from other sessions? Will there be a name
conflict
> > > when two sessions will try and execute the same SP?
> > >
> > > The more I get into it, the more questions I get. Hopefully I'll come
> out
> > > with good knowledge! :)
> > >
> > > Steve
> > >
> > > "Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid>
> > wrote
> > > in message news:uCXtqSrWEHA.2520@.TK2MSFTNGP12.phx.gbl...
> > > > As Vyas pointed out, the issue is that you have to create the
function
> > in
> > > > tempdb. I think he is right in suggesting that you can't use UDFs
that
> > > > reside in a different database in the definition of a table.
> > > >
> > >
> > >
> >
> >
>

Computed columns

Hi,

Consider the following example

create table sample

(col1 int,

col2 int ,

col3 AS col1 + col2) PERSISTED NOT NULL)

basically col3 is a computed column. Now when ever a row in col1 or col2 is updated the computed column will reflect the new value. how does this happen in the background. does this use row level triggers or what other mechanism is used to maintain col3 - computed column

the value does not exist by default

when you select a record and you included the calculated column

the server reevaluates everything which

is one of the great disadvantage of the computed column.

computation is being done all over again when you select from this column

|||I have marked it PERSISTED ...meaning that the column is saved in the database. I think what you are talking about is the computed column without using the PERSISTED key word.|||

sorry i wasn't aware of that new feature

any way

Their values are updated when any columns that are part of their calculation change

|||I would say magic :) Seriously, it would be done at a physical implementation level below what we have access to, much like values in an index get maintained. I think if you thought of it sort of like a row-level trigger it wouldn't be "wrong," but it is not through any mechanism that we have direct access to for sure.|||the reason why i posted the question is : If i use a lot of computed columns in my database, will it cause any kind of performance problems. so far what ever i have read about computed columns, no where it is mentioned that using computed columns may cause performance problems. Please let me know if you have any information.|||I think the question is more about how you need the data. There will be a performance hit, whether you persist them, or not. The difference will be based on whether you modify data more, or read it more. If it is a frequently used column (or might be) try persisting it. If it doesn't slow you down too much then that would be the best idea. Unless your formula is tremendously complex, I doubt you will even notice.|||

one good thing in answering in this forum is that you

got to learn new things. anyway here's what BOL has to say

Unless otherwise specified, computed columns are virtual columns that are not physically stored in the table. Their values are recalculated every time they are referenced in a query. The SQL Server 2005 Database Engine uses the PERSISTED keyword in the CREATE TABLE and ALTER TABLE statements to physically store computed columns in the table. Their values are updated when any columns that are part of their calculation change. By marking a computed column as PERSISTED, you can create an index on a computed column that is deterministic but not precise. Additionally, if a computed column references a CLR function, the Database Engine cannot verify whether the function is truly deterministic. In this case, the computed column must be PERSISTED so that indexes can be created on it.

In terms of performance therefore persisted columns, performs better than non-persisted. Except of course when we hard talking of harddisk consumption. persisted column may even outrun a column-trigger solution and the first is easier to maintain over the later.

|||Thanks for all your replies.

Thursday, March 8, 2012

Complexx AutoIncrment colum

Hi,

I have a situation where I have to maintain auto incremental values based on values of other colum. Sample table is given below.

colA

colB

colC

1

1

1

1

1

2

1

1

3

1

1

4

1

2

1

1

2

2

1

2

3

2

1

1

2

1

2

2

1

3

Now in above table I want to autoincrement value of colC on the basis of Values of colA+colB. Is there any way I can set this fuctionalty directky on table schema.

Nilkkanth Desai

There is no way to create this kind of functionality on the table schema.

You could use a TRIGGER.

|||

You can use computed column for ColC.

Thanks

Naras.

|||

Hello Narasimhan Jayachandran ,

Thanks for your reply. I tried to findout samples on computed columns but I did not find any. Can u give me sample on this issue. or give me more details link if any on this topic. Your sample code will be more helpful for me to solve this issue.

Thank you,

Nilkanth Desai


|||

It is not good idea to use the computed columns here.. (in other words you can't achive the result using the computed columns, since your computed colum is non-deterministic).

The best approach may be on the trigger or you can use new feature OUTPUT.

dafafadfa

Using OUTPUT:

Create table ABCTable
(C1 int, C2 Int, C3 INT)

Go

Begin Tran;
Begin Try

Declare @.Inserted Table(C1 int, C2 int);

Insert Into ABCTable(c1,C2)
OUTPUT Inserted.C1,Inserted.C2 Into @.Inserted
values (1,1)

Update ABCTable
Set
C3=Isnull(NewC3,1)
From
(
Select
Max(C3)+ 1 NewC3,
A.C1,
A.C2
From
ABCTable A
Join @.Inserted B On A.C1= B.C1 and A.c2=B.C2
Group By
A.C1, A.C2
)as NewC3
Where
ABCTable.C1 = NewC3.C1
And ABCTable.C2 = NewC3.C2
And C3 is NULL

IF @.@.ERROR = 0
Commit Tran
Else
Rollback Tran

End Try

Begin Catch
Rollback
End Catch

|||

Hi Nilkanth,

Computed column :

Example : create table tbcc ( c1 int, c2 int, c3 as (c1+c2))

insert tbcc (c1,c2) values (1,2)

select * from tbcc

c1 c2 c3
-- -- --
1 2 3

More details, refer Books on line under Create table

--
CREATE TABLE
[ database_name.[ owner ] . | owner. ] table_name
( { < column_definition >
| column_name AS computed_column_expression
| < table_constraint > ::= [ CONSTRAINT constraint_name ] }

| [ { PRIMARY KEY | UNIQUE } [ ,...n ]
) ..............

--

Thanks

Naras.

|||

Hi Narah,

Thanks for am alternative with sample code. It works.

Nilkanth

|||

Another question:

What is done if a row is deleted; are the rows supposed to auto-decrement?

|||

Hi,

It seems for update also it require one more trigger. Overall Triggers are costly in terms of resource utilisation. As this is my main trnsaction trble which handles 98% of traffic of the system I think triggers will be more heavy solution. Microsoft shall provide some mechenism in future release products as this kind of situation is mostlikely faced by most of the database developers. If we can find any schema based solution it will be light & easy to deploy.

Anyway bu this time this is the only solution I can use. If anyone finds more better solution then this please update this blog.

Nilkanth Desai

Complexx AutoIncrment colum

Hi,

I have a situation where I have to maintain auto incremental values based on values of other colum. Sample table is given below.

colA colB colC 1 1 1 1 1 2 1 1 3 1 1 4 1 2 1 1 2 2 1 2 3 2 1 1 2 1 2 2 1 3

Now in above table I want to autoincrement value of colC on the basis of Values of colA+colB. Is there any way I can set this fuctionalty directky on table schema.

Nilkkanth Desai

There is no way to create this kind of functionality on the table schema.

You could use a TRIGGER.

|||

You can use computed column for ColC.

Thanks

Naras.

|||

Hello Narasimhan Jayachandran ,

Thanks for your reply. I tried to findout samples on computed columns but I did not find any. Can u give me sample on this issue. or give me more details link if any on this topic. Your sample code will be more helpful for me to solve this issue.

Thank you,

Nilkanth Desai


|||

It is not good idea to use the computed columns here.. (in other words you can't achive the result using the computed columns, since your computed colum is non-deterministic).

The best approach may be on the trigger or you can use new feature OUTPUT.

dafafadfa

Using OUTPUT:

Create table ABCTable
(C1 int, C2 Int, C3 INT)

Go

Begin Tran;
Begin Try

Declare @.Inserted Table(C1 int, C2 int);

Insert Into ABCTable(c1,C2)
OUTPUT Inserted.C1,Inserted.C2 Into @.Inserted
values (1,1)

Update ABCTable
Set
C3=Isnull(NewC3,1)
From
(
Select
Max(C3)+ 1 NewC3,
A.C1,
A.C2
From
ABCTable A
Join @.Inserted B On A.C1= B.C1 and A.c2=B.C2
Group By
A.C1, A.C2
)as NewC3
Where
ABCTable.C1 = NewC3.C1
And ABCTable.C2 = NewC3.C2
And C3 is NULL

IF @.@.ERROR = 0
Commit Tran
Else
Rollback Tran

End Try

Begin Catch
Rollback
End Catch

|||

Hi Nilkanth,

Computed column :

Example : create table tbcc ( c1 int, c2 int, c3 as (c1+c2))

insert tbcc (c1,c2) values (1,2)

select * from tbcc

c1 c2 c3
-- -- --
1 2 3

More details, refer Books on line under Create table

--
CREATE TABLE
[ database_name.[ owner ] . | owner. ] table_name
( { < column_definition >
| column_name AS computed_column_expression
| < table_constraint > ::= [ CONSTRAINT constraint_name ] }

| [ { PRIMARY KEY | UNIQUE } [ ,...n ]
) ..............

--

Thanks

Naras.

|||

Hi Narah,

Thanks for am alternative with sample code. It works.

Nilkanth

|||

Another question:

What is done if a row is deleted; are the rows supposed to auto-decrement?

|||

Hi,

It seems for update also it require one more trigger. Overall Triggers are costly in terms of resource utilisation. As this is my main trnsaction trble which handles 98% of traffic of the system I think triggers will be more heavy solution. Microsoft shall provide some mechenism in future release products as this kind of situation is mostlikely faced by most of the database developers. If we can find any schema based solution it will be light & easy to deploy.

Anyway bu this time this is the only solution I can use. If anyone finds more better solution then this please update this blog.

Nilkanth Desai

Complex XML Parsing/Shredding

Hi,

I have generated a sample XML document from the XSD file using XMLSpy and used these in XML Source stage.

While I was parsing/Shredding the XML file and to write to sequential files I got the following error message.

The XML Source Adapter does not support mixed content model on Complex Types.

Additional Information:
Pipeline component has returned HRESULT error code )xC02092A1 from a method call. [Microsoft.SqlServer.DTSPipelineWrap]

Can anybody give a sample code with Complex XML type.

--

Find attached the XSD format. This is FYI.

Not sure I understand the question.

Empty, simple and element-only content models for complex types are supported, but mixed content is not.

Are you asking for a sample of a schema that has one of those content models?

|||

Are you able to get my XSD schema attached in my previous question? If not, could you please tell me how to send you the XSD schema

My schema content will be changed based on the instrument type.

For example,

<instrument type='Equity'>
<equityrelatedcolumns>value</equityrelatecolumns>
<pricinglist>
<price1/>
<price2/>
...
...
...
<pricen/>
</pricinglist>
</instrument>
<instrument type='Bond'>
<bondrelatedcolumns>value</bondrelatedcolumns>
<pricinglist>
<price1/>
<price2/>
...
...
...
<pricen/>
</pricinglist>
</instrument>

|||

Current SSIS support for loading XML does not support Complex Types with mixed content models. I looked at the schema provided and noticed several complex types with mixed="true" defined.

However, based on the XML sample provided, it is unclear to me why this is required since known of the element instances contained any non-element content. This would lead me to believe that at least fot the sample provided, you could change the content model on the ComplexTypes to not be mixed to work around the limitation of SSIS.

If this is not feasible, then you will have to preprocess te instance documents with an XSLT to turn the non-element content for the ComplexTypes with mixed content into element conent.

Hope that helps -

Andy

|||

Thanks Andy. The XSD file is the correct one. Our original XML file will contain mixed content type only.

We generated sample XML file through XMLSPY and specified NULL for all the contents. That is the reason the XML file looks like simple content.

Hope SSIS next version might look into this issue.

complex sql server 2005 query

Hi,
A sql server table is populated with records every 2 minutes. See below sample table
In the table, the Import_Date is a datetime field.

create table tblData
(
ID int identity(1, 1),
SourceID int,
SourceCode varchar(255)
Security varchar(255),
Bprice decimal(12, 8),
Aprice decimal(12, 8),
ImportDate datetime
)

Here is a populated table.
I have left gaps for better visual checks for you.

ID SourceID SourceCode Security Bprice BpriceSize Aprice ApriceSize ImportDate

1 1 sourceA SecA 100.2 2 99.12 1 2007-11-07 16:24:31.297
2 2 sourceW SecH 95.7 89.43 2007-11-07 16:24:31.297
3 3 SourceX SecS 50.56 1 76.44 4 2007-11-07 16:24:31.297
4 4 SourceQ SecZ 87.98 2007-11-07 16:24:31.297
5 5 SourceJ SecH 100.2 99.12 2 2007-11-07 16:24:31.297
6 6 SourceK SecU 2007-11-07 16:24:31.297
7 7 SourceT SecA 50.56 3 87.11 2007-11-07 16:24:31.297

8 1 sourceA SecA 100.2 6 99.12 2 2007-11-07 16:26:15.123
9 2 sourceW SecH 99.54 4 89.43 2007-11-07 16:26:15.123
10 3 SourceX SecS 50.56 2 19.33 2007-11-07 16:26:15.123
11 4 SourceQ SecZ 16.98 87.98 2007-11-07 16:26:15.123
12 5 SourceJ SecH 100.2 1 99.12 2 2007-11-07 16:26:15.123
13 6 SourceK SecU 2007-11-07 16:26:15.123
14 7 SourceT SecA 50.56 2 87.11 1 2007-11-07 16:26:15.123

15 1 sourceA SecA 100.2 1 87.11 1 2007-11-07 16:26:15.123
16 2 sourceW SecH 99.66 89.43 2 2007-11-07 16:26:15.123
17 3 SourceX SecS 50.56 2 19.33 2007-11-07 16:26:15.123
18 4 SourceQ SecZ 16.98 3 87.98 3 2007-11-07 16:26:15.123
19 5 SourceJ SecH 100.2 3 99.12 3 2007-11-07 16:26:15.123
20 6 SourceK SecU 2007-11-07 16:26:15.123
21 7 SourceT SecA 101.32 5 87.11 3 2007-11-07 16:26:15.123
...

I am trying to build a sql query to show which source is offering the max(Bprice) and who is offering the min(Aprice).
In addition if more than one sources are offering the same prices then they should be shown as shown below in the first record i.e. (SourceA, SourceT) --> 3 + 1 = 4
This is what I would like to see:

Security Max_Bprice Bprice_Size Bprice_SourceCode Min_Aprice Aprice_Size Aprice_SourceCode

SecA 101.32 5 SourceT 87.11 4 SourceA, SourceT
SecH 100.2 3 SourceJ 89.43 2 SourceW
SecS 50.56 2 SourceX 19.33 SourceX
SecZ 16.98 3 SourceQ 87.98 3 SourceQ

What is the sql query to do this please?

This is what I have started with but it is not correct...

select
Security,
max(Bprice) as 'Max_Bprice',
SourceCode as 'Bprice_SourceCode',
min(Aprice) as 'Min_Aprice',
SourceCode as 'Aprice_SourceCode'
from
tblData
group by
Security,
SourceCodeHi

You are almost there but not quite. Could you provide your sample data as point 3 here please:
http://www.dbforums.com/showthread.php?t=1196943

Also, your DDL does not match the data you have supplied.

Not really related, but there looks to be a third normal form issue here.

Cheers|||http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=92298|||Here it is.
Thanks

DECLARE @.Sample TABLE (ID INT, SourceID INT, SourceCode VARCHAR(20), Security VARCHAR(20), Bprice MONEY, BpriceSize INT, Aprice MONEY, ApriceSize INT, ImportDate DATETIME)

INSERT @.Sample
SELECT 1, 1, 'sourceA', 'SecA', 100.2 , 2, 99.12, 1, '2007-11-07 16:24:31.297' UNION ALL
SELECT 2, 2, 'sourceW', 'SecH', 95.7 , NULL, 89.43, NULL, '2007-11-07 16:24:31.297' UNION ALL
SELECT 3, 3, 'SourceX', 'SecS', 50.56, 1, 76.44, 4, '2007-11-07 16:24:31.297' UNION ALL
SELECT 4, 4, 'SourceQ', 'SecZ', 87.98, NULL, NULL, NULL, '2007-11-07 16:24:31.297' UNION ALL
SELECT 5, 5, 'SourceJ', 'SecH', 100.2 , NULL, 99.12, 2, '2007-11-07 16:24:31.297' UNION ALL
SELECT 6, 6, 'SourceK', 'SecU', NULL, NULL, NULL, NULL, '2007-11-07 16:24:31.297' UNION ALL
SELECT 7, 7, 'SourceT', 'SecA', 50.56, 3, 87.11, NULL, '2007-11-07 16:24:31.297' UNION ALL
SELECT 8, 1, 'sourceA', 'SecA', 100.2 , 6, 99.12, 2, '2007-11-07 16:26:15.123' UNION ALL
SELECT 9, 2, 'sourceW', 'SecH', 99.54, 4, 89.43, NULL, '2007-11-07 16:26:15.123' UNION ALL
SELECT 10, 3, 'SourceX', 'SecS', 50.56, 2, 19.33, NULL, '2007-11-07 16:26:15.123' UNION ALL
SELECT 11, 4, 'SourceQ', 'SecZ', 16.98, NULL, 87.98, NULL, '2007-11-07 16:26:15.123' UNION ALL
SELECT 12, 5, 'SourceJ', 'SecH', 100.2 , 1, 99.12, 2, '2007-11-07 16:26:15.123' UNION ALL
SELECT 13, 6, 'SourceK', 'SecU', NULL, NULL, NULL, NULL, '2007-11-07 16:26:15.123' UNION ALL
SELECT 14, 7, 'SourceT', 'SecA', 50.56, 2, 87.11, 1, '2007-11-07 16:26:15.123' UNION ALL
SELECT 15, 1, 'sourceA', 'SecA', 100.2 , 1, 87.11, 1, '2007-11-07 16:26:15.123' UNION ALL
SELECT 16, 2, 'sourceW', 'SecH', 99.66, NULL, 89.43, 2, '2007-11-07 16:26:15.123' UNION ALL
SELECT 17, 3, 'SourceX', 'SecS', 50.56, 2, 19.33, NULL, '2007-11-07 16:26:15.123' UNION ALL
SELECT 18, 4, 'SourceQ', 'SecZ', 16.98, 3, 87.98, 3, '2007-11-07 16:26:15.123' UNION ALL
SELECT 19, 5, 'SourceJ', 'SecH', 100.2 , 3, 99.12, 3, '2007-11-07 16:26:15.123' UNION ALL
SELECT 20, 6, 'SourceK', 'SecU', NULL , NULL, NULL, NULL, '2007-11-07 16:26:15.123' UNION ALL
SELECT 21, 7, 'SourceT', 'SecA', 101.32, 5, 87.11, 3, '2007-11-07 16:26:15.123'|||I'll leave it to Peso. He'll get it soon enough I would imagine.|||Thanks anyway|||I suggest that you break it up

Do one, then the other, then combine them

I think you can do a union or a join of 2 derived table.

Since each derived table is going to be a single row, you wont have to worry about a cartesian product

Friday, February 24, 2012

Complex counting, joining and grouping

I have a sales stats table and I want to do some counting and grouping by
source and cluster (join required).
The 2 tables and sample data are as follows:
CREATE TABLE
dbo.stat (id_source varchar(10) not null,
id_period int not null,
id_dept varchar(10) not null,
ind_Domestic char(1) not null,
trade_count int not null)
CREATE TABLE dbo.stat_Hierarchy(id_dept varchar(5) not null,
id_cluster varchar(10) not null)
INSERT INTO stat VALUES('INTERNET',200601,'N41','Y',250)
INSERT INTO stat VALUES('INTERNET',200601,'N41','N',100)
INSERT INTO stat VALUES('INTERNET',200601,'S51','Y',200)
INSERT INTO stat VALUES('INTERNET',200601,'S51','N',120)
INSERT INTO stat VALUES('INTERNET',200601,'021','Y',50)
INSERT INTO stat VALUES('INTERNET',200601,'021','N',70)
INSERT INTO stat VALUES('INTERNET',200601,'131','Y',30)
INSERT INTO stat VALUES('INTERNET',200601,'131','N',70)
INSERT INTO stat VALUES('STORE',200601,'00P','Y',130)
INSERT INTO stat VALUES('STORE',200601,'00P','N',1)
INSERT INTO stat VALUES('STORE',200601,'00S','N',100)
INSERT INTO stat VALUES('STORE',200601,'N41','Y',130)
INSERT INTO stat VALUES('STORE',200601,'N41','N',250)
INSERT INTO stat VALUES('STORE',200601,'S51','Y',110)
INSERT INTO stat VALUES('STORE',200601,'S51','N',320)
INSERT INTO stat VALUES('STORE',200601,'021','Y',30)
INSERT INTO stat VALUES('STORE',200601,'021','N',40)
INSERT INTO stat VALUES('AGENCY',200601,'0101','Y',50)
INSERT INTO stat VALUES('AGENCY',200601,'0101','N',10)
INSERT INTO stat VALUES('AGENCY',200601,'0100300','Y',100
)
INSERT INTO stat VALUES('AGENCY',200601,'0100300','N',320
)
INSERT INTO stat VALUES('AGENCY',200601,'021','Y',150)
INSERT INTO stat VALUES('AGENCY',200601,'021','N',50)
INSERT INTO stat VALUES('AGENCY',200601,'131','Y',20)
INSERT INTO stat VALUES('AGENCY',200601,'131','N',80)
INSERT INTO stat_Hierarchy VALUES('00P','IB_CL_OTHE')
INSERT INTO stat_Hierarchy VALUES('00S','IB_CL_OTHE')
INSERT INTO stat_Hierarchy VALUES('0101','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('021','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('131','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('0100300','HV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('N41','HV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('S51','HV_CL_SALE')
And I want the data to be returned like so:
id_cluster,id_source,ind_Domestic,trade_
count
HV_CL_SALE,INTERNET,N,220
HV_CL_SALE,INTERNET,Y,450
HV_CL_SALE,STORE,N,570
HV_CL_SALE,STORE,Y,240
IB_CL_OTHE,STORE,N,101
IB_CL_OTHE,STORE,Y,130
LV_CL_SALE,AGENCY,N,140
LV_CL_SALE,AGENCY,Y,200
LV_CL_SALE,INTERNET,N,140
LV_CL_SALE,INTERNET,Y,80
LV_CL_SALE,STORE,N,40
LV_CL_SALE,STORE,Y,30
How could I do this?select
h.id_cluster,
s.id_source,
s.ind_Domestic,
sum(s.trade_count) as trade_count
from stat s
inner join stat_Hierarchy h on h.id_dept=s.id_dept
group by h.id_cluster,s.id_source,s.ind_Domestic
order by h.id_cluster,s.id_source,s.ind_Domestic

Complex counting, joining and grouping

I have a sales stats table and I want to do some counting and grouping by
source and cluster (join required).
The 2 tables and sample data are as follows:
CREATE TABLE
dbo.stat (id_source varchar(10) not null,
id_period int not null,
id_dept varchar(10) not null,
ind_Domestic char(1) not null,
trade_count int not null)
CREATE TABLE dbo.stat_Hierarchy(id_dept varchar(5) not null,
id_cluster varchar(10) not null)
INSERT INTO stat VALUES('INTERNET',200601,'N41','Y',250)
INSERT INTO stat VALUES('INTERNET',200601,'N41','N',100)
INSERT INTO stat VALUES('INTERNET',200601,'S51','Y',200)
INSERT INTO stat VALUES('INTERNET',200601,'S51','N',120)
INSERT INTO stat VALUES('INTERNET',200601,'021','Y',50)
INSERT INTO stat VALUES('INTERNET',200601,'021','N',70)
INSERT INTO stat VALUES('INTERNET',200601,'131','Y',30)
INSERT INTO stat VALUES('INTERNET',200601,'131','N',70)
INSERT INTO stat VALUES('STORE',200601,'00P','Y',130)
INSERT INTO stat VALUES('STORE',200601,'00P','N',1)
INSERT INTO stat VALUES('STORE',200601,'00S','N',100)
INSERT INTO stat VALUES('STORE',200601,'N41','Y',130)
INSERT INTO stat VALUES('STORE',200601,'N41','N',250)
INSERT INTO stat VALUES('STORE',200601,'S51','Y',110)
INSERT INTO stat VALUES('STORE',200601,'S51','N',320)
INSERT INTO stat VALUES('STORE',200601,'021','Y',30)
INSERT INTO stat VALUES('STORE',200601,'021','N',40)
INSERT INTO stat VALUES('AGENCY',200601,'0101','Y',50)
INSERT INTO stat VALUES('AGENCY',200601,'0101','N',10)
INSERT INTO stat VALUES('AGENCY',200601,'0100300','Y',100)
INSERT INTO stat VALUES('AGENCY',200601,'0100300','N',320)
INSERT INTO stat VALUES('AGENCY',200601,'021','Y',150)
INSERT INTO stat VALUES('AGENCY',200601,'021','N',50)
INSERT INTO stat VALUES('AGENCY',200601,'131','Y',20)
INSERT INTO stat VALUES('AGENCY',200601,'131','N',80)
INSERT INTO stat_Hierarchy VALUES('00P','IB_CL_OTHE')
INSERT INTO stat_Hierarchy VALUES('00S','IB_CL_OTHE')
INSERT INTO stat_Hierarchy VALUES('0101','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('021','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('131','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('0100300','HV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('N41','HV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('S51','HV_CL_SALE')
And I want the data to be returned like so:
id_cluster,id_source,ind_Domestic,trade_count
HV_CL_SALE,INTERNET,N,220
HV_CL_SALE,INTERNET,Y,450
HV_CL_SALE,STORE,N,570
HV_CL_SALE,STORE,Y,240
IB_CL_OTHE,STORE,N,101
IB_CL_OTHE,STORE,Y,130
LV_CL_SALE,AGENCY,N,140
LV_CL_SALE,AGENCY,Y,200
LV_CL_SALE,INTERNET,N,140
LV_CL_SALE,INTERNET,Y,80
LV_CL_SALE,STORE,N,40
LV_CL_SALE,STORE,Y,30
How could I do this?
select
h.id_cluster,
s.id_source,
s.ind_Domestic,
sum(s.trade_count) as trade_count
from stat s
inner join stat_Hierarchy h on h.id_dept=s.id_dept
group by h.id_cluster,s.id_source,s.ind_Domestic
order by h.id_cluster,s.id_source,s.ind_Domestic

Complex counting, joining and grouping

I have a sales stats table and I want to do some counting and grouping by
source and cluster (join required).
The 2 tables and sample data are as follows:
CREATE TABLE
dbo.stat (id_source varchar(10) not null,
id_period int not null,
id_dept varchar(10) not null,
ind_Domestic char(1) not null,
trade_count int not null)
CREATE TABLE dbo.stat_Hierarchy(id_dept varchar(5) not null,
id_cluster varchar(10) not null)
INSERT INTO stat VALUES('INTERNET',200601,'N41','Y',250)
INSERT INTO stat VALUES('INTERNET',200601,'N41','N',100)
INSERT INTO stat VALUES('INTERNET',200601,'S51','Y',200)
INSERT INTO stat VALUES('INTERNET',200601,'S51','N',120)
INSERT INTO stat VALUES('INTERNET',200601,'021','Y',50)
INSERT INTO stat VALUES('INTERNET',200601,'021','N',70)
INSERT INTO stat VALUES('INTERNET',200601,'131','Y',30)
INSERT INTO stat VALUES('INTERNET',200601,'131','N',70)
INSERT INTO stat VALUES('STORE',200601,'00P','Y',130)
INSERT INTO stat VALUES('STORE',200601,'00P','N',1)
INSERT INTO stat VALUES('STORE',200601,'00S','N',100)
INSERT INTO stat VALUES('STORE',200601,'N41','Y',130)
INSERT INTO stat VALUES('STORE',200601,'N41','N',250)
INSERT INTO stat VALUES('STORE',200601,'S51','Y',110)
INSERT INTO stat VALUES('STORE',200601,'S51','N',320)
INSERT INTO stat VALUES('STORE',200601,'021','Y',30)
INSERT INTO stat VALUES('STORE',200601,'021','N',40)
INSERT INTO stat VALUES('AGENCY',200601,'0101','Y',50)
INSERT INTO stat VALUES('AGENCY',200601,'0101','N',10)
INSERT INTO stat VALUES('AGENCY',200601,'0100300','Y',100)
INSERT INTO stat VALUES('AGENCY',200601,'0100300','N',320)
INSERT INTO stat VALUES('AGENCY',200601,'021','Y',150)
INSERT INTO stat VALUES('AGENCY',200601,'021','N',50)
INSERT INTO stat VALUES('AGENCY',200601,'131','Y',20)
INSERT INTO stat VALUES('AGENCY',200601,'131','N',80)
INSERT INTO stat_Hierarchy VALUES('00P','IB_CL_OTHE')
INSERT INTO stat_Hierarchy VALUES('00S','IB_CL_OTHE')
INSERT INTO stat_Hierarchy VALUES('0101','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('021','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('131','LV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('0100300','HV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('N41','HV_CL_SALE')
INSERT INTO stat_Hierarchy VALUES('S51','HV_CL_SALE')
And I want the data to be returned like so:
id_cluster,id_source,ind_Domestic,trade_count
HV_CL_SALE,INTERNET,N,220
HV_CL_SALE,INTERNET,Y,450
HV_CL_SALE,STORE,N,570
HV_CL_SALE,STORE,Y,240
IB_CL_OTHE,STORE,N,101
IB_CL_OTHE,STORE,Y,130
LV_CL_SALE,AGENCY,N,140
LV_CL_SALE,AGENCY,Y,200
LV_CL_SALE,INTERNET,N,140
LV_CL_SALE,INTERNET,Y,80
LV_CL_SALE,STORE,N,40
LV_CL_SALE,STORE,Y,30
How could I do this?select
h.id_cluster,
s.id_source,
s.ind_Domestic,
sum(s.trade_count) as trade_count
from stat s
inner join stat_Hierarchy h on h.id_dept=s.id_dept
group by h.id_cluster,s.id_source,s.ind_Domestic
order by h.id_cluster,s.id_source,s.ind_Domestic

Complex calculation!!

Hi
I have got two tables and have one to many relationship on them.
i ll show you the sample data


Table 1
ID DoctorName Practice Full_Time Cost Expenditure
1 ABC qw yes $100
2 ABD wer no $566
3 ZXA ddf yes $22
........................

table 2
Practices
qw
wer
ffgg
hhjk
ddf
..
The scenario is like this
a doctor can work in more than one practice. and he can be a full time practioner or a part time practitioner

The problem is if a doctor is a full time practioner in 1 service and a part time practitioner in another, 75% of cost goes to first practice and the rest 25% goes to other

if the doctor is non full time in more than one practice, the cost gets distributed equally in all practices.

how shall i do this?
pls HELP!!

Moving to the T-SQL forum.

Complex calculation!!

Hi
I have got two tables and have one to many relationship on them.
i ll show you the sample data


Table 1
ID DoctorName Practice Full_Time Cost Expenditure
1 ABC qw yes $100
2 ABD wer no $566
3 ZXA ddf yes $22
........................

table 2
Practices
qw
wer
ffgg
hhjk
ddf
..
The scenario is like this
a doctor can work in more than one practice. and he can be a full time practioner or a part time practitioner

The problem is if a doctor is a full time practioner in 1 service and a part time practitioner in another, 75% of cost goes to first practice and the rest 25% goes to other

if the doctor is non full time in more than one practice, the cost gets distributed equally in all practices.

how shall i do this?
pls HELP!!

Moving to the T-SQL forum.

Sunday, February 19, 2012

compiler -- newbie q

Hi,
I am very new to xml. i have downloaded some sample data from MS and there
is file in there called: CustService.sln
I am required to comile this file. How do I compile it? I dont have a
comiler on my computer.
Thanks,
A .sln file is a Visual Studio .NET solution file. I suspect the example
assumes you have Visual Studio installed.
Cheers,
Graeme
--
Graeme Malcolm
Principal Technologist
Content Master Ltd.
www.contentmaster.com
www.microsoft.com/mspress/books/6137.asp
"Hasan Kachal" <kachal@.nospam.com> wrote in message
news:Os4q60LTEHA.2544@.TK2MSFTNGP10.phx.gbl...
Hi,
I am very new to xml. i have downloaded some sample data from MS and there
is file in there called: CustService.sln
I am required to comile this file. How do I compile it? I dont have a
comiler on my computer.
Thanks,

Friday, February 10, 2012

Comparing within a Self Join

Greetings All,
Have a table with the following sample data
select * from t1
PatientName RxFillDate
JACK 01/01/2006
JACK 02/01/2006
JACK 03/01/2006
JILL 04/04/2006
JILL 05/25/2006
JILL 06/25/2006
Here is what my objective is..
I want to output the PatientNames who have a Difference of more than 30 days
in any of their RxFillDate.
So in the above Sample I would just get JILL as the Output , Since the
difference
between one pair of he RxFillDate ( 04/04/2006 and 05/25/2006 ) is more than
30 Days.
Hope I explained it clearly - Any help will be appreciated.
Thanks in advance,
AbTry:
select distinct
x.PatientName
from
t1 x
join
t2 y on y.PatientName = x.PatientName
and y.RxFillDate > x.RxFillDate +30
and not exists
(
select
*
from
t1 z
where
z.PatientName = x.PatientName
and z.RxFillDate between x.RxFillDate and y.RxFillDate
)
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Ab" <Ab@.discussions.microsoft.com> wrote in message
news:17A7AA22-364E-46CE-97E6-7FC128C888AE@.microsoft.com...
Greetings All,
Have a table with the following sample data
select * from t1
PatientName RxFillDate
JACK 01/01/2006
JACK 02/01/2006
JACK 03/01/2006
JILL 04/04/2006
JILL 05/25/2006
JILL 06/25/2006
Here is what my objective is..
I want to output the PatientNames who have a Difference of more than 30 days
in any of their RxFillDate.
So in the above Sample I would just get JILL as the Output , Since the
difference
between one pair of he RxFillDate ( 04/04/2006 and 05/25/2006 ) is more than
30 Days.
Hope I explained it clearly - Any help will be appreciated.
Thanks in advance,
Ab|||Tom, Thanks for your reply
Will this also work for in situation as below ( maybe i should have
mentioned it before)

> select * from t1
> PatientName RxFillDate
> JACK 01/01/2006
> JACK 02/01/2006
> JACK 03/01/2006
> JILL 04/04/2006
> JILL 05/25/2006
> JILL 06/25/2006
> JILL 06/28/2006
>ROCKY 04/01/2006
>MARK 05/03/2006
In the above - again Patient JILL would showup in the output..
"Tom Moreau" wrote:

> Try:
> select distinct
> x.PatientName
> from
> t1 x
> join
> t2 y on y.PatientName = x.PatientName
> and y.RxFillDate > x.RxFillDate +30
> and not exists
> (
> select
> *
> from
> t1 z
> where
> z.PatientName = x.PatientName
> and z.RxFillDate between x.RxFillDate and y.RxFillDate
> )
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
> SQL Server MVP
> Columnist, SQL Server Professional
> Toronto, ON Canada
> www.pinpub.com
> ..
> "Ab" <Ab@.discussions.microsoft.com> wrote in message
> news:17A7AA22-364E-46CE-97E6-7FC128C888AE@.microsoft.com...
> Greetings All,
> Have a table with the following sample data
> select * from t1
> PatientName RxFillDate
> JACK 01/01/2006
> JACK 02/01/2006
> JACK 03/01/2006
> JILL 04/04/2006
> JILL 05/25/2006
> JILL 06/25/2006
> Here is what my objective is..
> I want to output the PatientNames who have a Difference of more than 30 da
ys
> in any of their RxFillDate.
> So in the above Sample I would just get JILL as the Output , Since the
> difference
> between one pair of he RxFillDate ( 04/04/2006 and 05/25/2006 ) is more th
an
> 30 Days.
> Hope I explained it clearly - Any help will be appreciated.
> Thanks in advance,
> Ab
>|||She should show up, since there was at least one period of time where she
waited longer than 30 days to get her meds. What's the exact requirement?
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Ab" <Ab@.discussions.microsoft.com> wrote in message
news:4BAC8463-D694-43F3-83F7-AB0E538F3954@.microsoft.com...
Tom, Thanks for your reply
Will this also work for in situation as below ( maybe i should have
mentioned it before)

> select * from t1
> PatientName RxFillDate
> JACK 01/01/2006
> JACK 02/01/2006
> JACK 03/01/2006
> JILL 04/04/2006
> JILL 05/25/2006
> JILL 06/25/2006
> JILL 06/28/2006
>ROCKY 04/01/2006
>MARK 05/03/2006
In the above - again Patient JILL would showup in the output..
"Tom Moreau" wrote:

> Try:
> select distinct
> x.PatientName
> from
> t1 x
> join
> t2 y on y.PatientName = x.PatientName
> and y.RxFillDate > x.RxFillDate +30
> and not exists
> (
> select
> *
> from
> t1 z
> where
> z.PatientName = x.PatientName
> and z.RxFillDate between x.RxFillDate and y.RxFillDate
> )
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
> SQL Server MVP
> Columnist, SQL Server Professional
> Toronto, ON Canada
> www.pinpub.com
> ..
> "Ab" <Ab@.discussions.microsoft.com> wrote in message
> news:17A7AA22-364E-46CE-97E6-7FC128C888AE@.microsoft.com...
> Greetings All,
> Have a table with the following sample data
> select * from t1
> PatientName RxFillDate
> JACK 01/01/2006
> JACK 02/01/2006
> JACK 03/01/2006
> JILL 04/04/2006
> JILL 05/25/2006
> JILL 06/25/2006
> Here is what my objective is..
> I want to output the PatientNames who have a Difference of more than 30
> days
> in any of their RxFillDate.
> So in the above Sample I would just get JILL as the Output , Since the
> difference
> between one pair of he RxFillDate ( 04/04/2006 and 05/25/2006 ) is more
> than
> 30 Days.
> Hope I explained it clearly - Any help will be appreciated.
> Thanks in advance,
> Ab
>|||select distinct t1.PatientName
from t1
where dateadd(day, 30, RxFillDate) < (
select min(t2.RxFillDate) from t1 t2
where t1.PatientName = t2.PatientName
and t1.RxFillDate < t2.RxFillDate)|||Thanks for your postings...But somehow I could not get the correct results.
Also I have the requirement now in much more detail...
Thanks for your responses and help.
We have a table with following information.
MemberName FillDate DaysSupply
PETER GAMBINI 09/22/05 30
PETER GAMBINI 09/24/05 30
PETER GAMBINI 12/25/05 30
MARIA ROSA 10/03/05 15
MARIA ROSA 10/18/05 30
MARIA ROSA 11/18/05 30
I am trying to find out Members with multiple Claims who had any lapses in
the Fill Dates plus a lag of 15 Days.
For eg: Member PETER GAMBINI in the 2nd record has a
Fill Date of 09/24/05 with Days Supply as 30.
and his next FillDate is 12/25/05
So ( 12/25/05 - 09/24/05 ) - 30 > 15 (Lag Days) is TRUE so Member PETER
GAMBINI would show up in my Select and hence in the Report.
The above is not true for MARIA ROSA so she would not be selected in my Quer
y.
because
...
MARIA ROSA 10/03/05 15
MARIA ROSA 10/18/05 30
MARIA ROSA 11/18/05 30
...
Her First FillDate Days Supply is 15 so
(10/18/05 - 10/03/05 ) - 15 > 15 is False
Next iteration
Her First FillDate Days Supply is 30 so
(11/18/05 - 10/08/05 ) - 30 > 15 is False
So she would not show up in the result my Query.
Hope this helps in understanding the problem..thanks in advance.
"Alexander Kuznetsov" wrote:

> select distinct t1.PatientName
> from t1
> where dateadd(day, 30, RxFillDate) < (
> select min(t2.RxFillDate) from t1 t2
> where t1.PatientName = t2.PatientName
> and t1.RxFillDate < t2.RxFillDate)
>|||The example below should be fine, you just need to replace the literal 30
with DaysSupply.
select distinct t1.PatientName
from t1
where dateadd(day, t1.DaysSupply, t1.RxFillDate) < (
select min(t2.RxFillDate) from t1 t2
where t1.PatientName = t2.PatientName
and t1.RxFillDate < t2.RxFillDate)
"Kanti Gala" <KantiGala@.discussions.microsoft.com> wrote in message
news:9BFEAEFA-9830-4DC9-A327-02564E4D10FE@.microsoft.com...
> Thanks for your postings...But somehow I could not get the correct
results.
> Also I have the requirement now in much more detail...
> Thanks for your responses and help.
> We have a table with following information.
> MemberName FillDate DaysSupply
> PETER GAMBINI 09/22/05 30
> PETER GAMBINI 09/24/05 30
> PETER GAMBINI 12/25/05 30
> MARIA ROSA 10/03/05 15
> MARIA ROSA 10/18/05 30
> MARIA ROSA 11/18/05 30
> I am trying to find out Members with multiple Claims who had any lapses in
> the Fill Dates plus a lag of 15 Days.
> For eg: Member PETER GAMBINI in the 2nd record has a
> Fill Date of 09/24/05 with Days Supply as 30.
> and his next FillDate is 12/25/05
> So ( 12/25/05 - 09/24/05 ) - 30 > 15 (Lag Days) is TRUE so Member PETER
> GAMBINI would show up in my Select and hence in the Report.
> The above is not true for MARIA ROSA so she would not be selected in my
Query.
> because
> ...
> MARIA ROSA 10/03/05 15
> MARIA ROSA 10/18/05 30
> MARIA ROSA 11/18/05 30
> ...
> Her First FillDate Days Supply is 15 so
> (10/18/05 - 10/03/05 ) - 15 > 15 is False
> Next iteration
> Her First FillDate Days Supply is 30 so
> (11/18/05 - 10/08/05 ) - 30 > 15 is False
> So she would not show up in the result my Query.
> Hope this helps in understanding the problem..thanks in advance.
>
> "Alexander Kuznetsov" wrote:
>