Tuesday, March 20, 2012
Can not call SET ROWCOUNT in a UDF - Example
gives the error
CREATE FUNCTION KUKU()
RETURNS INT
BEGIN
SET ROWCOUNT 1
return 1
END
"Boaz Ben-Porat" <bbp@.milestone.dk> wrote in message
news:O2BhOfTFGHA.3200@.tk2msftngp13.phx.gbl...
> When I call SET ROWCOUNT <number> inside a User Defined Function I get the
> following error:
> Error 443: Invalid use of 'UNKNOWN TOKEN' within a function.
> I couldn't find anything about this error in the BOL.
> Any solution ?
> TIA
> Boaz Ben-Porat
> Milestone systems
> Debmark
>A scalar UDF, by definition, returns exactly one "row", so what do you need
to override it for, and what do you consider to be a "row"? This returns a
single value... there are no rows or columns...
"Boaz Ben-Porat" <bbp@.milestone.dk> wrote in message
news:eqhochTFGHA.216@.TK2MSFTNGP15.phx.gbl...
>I forgot to supply an example. The following is a minimal function that
>gives the error
> CREATE FUNCTION KUKU()
> RETURNS INT
> BEGIN
> SET ROWCOUNT 1
> return 1
> END
>
>
> "Boaz Ben-Porat" <bbp@.milestone.dk> wrote in message
> news:O2BhOfTFGHA.3200@.tk2msftngp13.phx.gbl...
>
Can not call SET ROWCOUNT in a UDF
following error:
Error 443: Invalid use of 'UNKNOWN TOKEN' within a function.
I couldn't find anything about this error in the BOL.
Any solution ?
TIA
Boaz Ben-Porat
Milestone systems
DebmarkUse the SET directive before calling the function?
SET NOCOUNT ON
SELECT * FROM dbo.function()
?
"Boaz Ben-Porat" <bbp@.milestone.dk> wrote in message
news:O2BhOfTFGHA.3200@.tk2msftngp13.phx.gbl...
> When I call SET ROWCOUNT <number> inside a User Defined Function I get the
> following error:
> Error 443: Invalid use of 'UNKNOWN TOKEN' within a function.
> I couldn't find anything about this error in the BOL.
> Any solution ?
> TIA
> Boaz Ben-Porat
> Milestone systems
> Debmark
>|||Seems that SET ROWCOUNT isn't allowed in a UDF.
If you're using QA with syntax coloring, note that ROWCOUNT is not
colored as a keyword, so that would be why the 'UNKNOWN TOKEN' is being
returned.
Boaz Ben-Porat wrote:
> When I call SET ROWCOUNT <number> inside a User Defined Function I get the
> following error:
> Error 443: Invalid use of 'UNKNOWN TOKEN' within a function.
> I couldn't find anything about this error in the BOL.
> Any solution ?
> TIA
> Boaz Ben-Porat
> Milestone systems
> Debmark
>|||correction: SET <OPTION> ON is not allowed in a UDF. (set rowcount, set
nocount, etc.)
Trey Walpole wrote:
> Seems that SET ROWCOUNT isn't allowed in a UDF.
> If you're using QA with syntax coloring, note that ROWCOUNT is not
> colored as a keyword, so that would be why the 'UNKNOWN TOKEN' is being
> returned.
> Boaz Ben-Porat wrote:
>|||Hello, Boaz
You cannot use SET ROWCOUNT in a UDF, because a function cannot contain
statements that cause side-effects. If you wish to use a constant for
SET ROWCOUNT, you can use the "TOP n" clause, instead. If you want to
use a variable (or a parameter) you can use the "TOP (n)" clause in SQL
Server 2005 (but I understand that you use SQL Server 2000). If you
cannot upgrade to SQL Server 2005, it may be possible to use a
subquery, but it will be slower (you should consider using a stored
procedure instead of a function).
Razvan
Thursday, March 8, 2012
Can IIF() function be used in SELECT statment?
difination.
SELECT iif( month(ref_date)=1 , 1 , 0 )
FROM MyTable
But the system always show me
Incorrect syntax near '='
What's the corect syntax of iif function?
Thank you.Hi!
There is no IIF function in T-SQL, but you can use CASE expression instead.
Do please look for the syntax in Books OnLine.
Dejan Sarka, SQL Server MVP
Associate Mentor
Solid Quality Learning
More than just Training
www.SolidQualityLearning.com
"Xiaotao, Lu" <luxiaotao@.hotmail.com> wrote in message
news:eLm9ieCAEHA.1608@.TK2MSFTNGP11.phx.gbl...
> I try to use IIF() function in SELECT statmenmt in a store procedure
> difination.
> SELECT iif( month(ref_date)=1 , 1 , 0 )
> FROM MyTable
> But the system always show me
> Incorrect syntax near '='
> What's the corect syntax of iif function?
> Thank you.
>|||Use CASE:
SELECT CASE WHEN MONTH(ref_date)=1 THEN 1 ELSE 0 END
FROM MyTable
David Portas
SQL Server MVP
--
Can IIF() function be used in SELECT statment?
difination.
SELECT iif( month(ref_date)=1 , 1 , 0 )
FROM MyTable
But the system always show me
Incorrect syntax near '='
What's the corect syntax of iif function?
Thank you.Hi!
There is no IIF function in T-SQL, but you can use CASE expression instead.
Do please look for the syntax in Books OnLine.
--
Dejan Sarka, SQL Server MVP
Associate Mentor
Solid Quality Learning
More than just Training
www.SolidQualityLearning.com
"Xiaotao, Lu" <luxiaotao@.hotmail.com> wrote in message
news:eLm9ieCAEHA.1608@.TK2MSFTNGP11.phx.gbl...
> I try to use IIF() function in SELECT statmenmt in a store procedure
> difination.
> SELECT iif( month(ref_date)=1 , 1 , 0 )
> FROM MyTable
> But the system always show me
> Incorrect syntax near '='
> What's the corect syntax of iif function?
> Thank you.
>|||Use CASE:
SELECT CASE WHEN MONTH(ref_date)=1 THEN 1 ELSE 0 END
FROM MyTable
--
David Portas
SQL Server MVP
--
can I use the value of field in last record?
I handle a output by a function with three parameters
the first is a value of Field [a1] in current record,
the second is value of Field [a1] in last record,
the third is value of Field [a1] in next record,
is it possible in RS2005?
Yes you can use them in a given scope (table/list):
Fields!a1.Value
Last(Fields!a1.Value, "Dataset1")
Fields!a1.Value(RowNumber("Dataset1")+1)
Shyam
Wednesday, March 7, 2012
Can I use SQL instead of cursor?
I'm currently converting a VB function to SQL-Server. The function uses a cursor to find the "terms of delivery" (TOD) with the highest priority.
I have a table with articlenumber, tod (and lots of other columns that doesn't matter now)
ABC123 , AFG
ABC123 , AFG
ABC123 , BGH
ABC123 , BGH
ABC123 , CDD
"CDD" has the highest priority and therefore ALL with the same articlenumber should use that tod.
The existing function uses a cursor and loops through a recordset and updates every row with the same articlenumber as the current row with the tod with the highest priority (of the ones read) with the same articlenumber.
One update per row takes "forever" to run...
I figured it would be possible to select the tod with the highest priority for one articlenumber into a temp table and then do ONE update to set the tod on all rows...Here is a quick and dirty. It could be improved with a little work:
create table foo (ArtNo varchar(25), pri varchar(25))
insert into foo values ('ABC123' , 'AFG')
insert into foo values ('ABC123' , 'AFG')
insert into foo values ('ABC123' , 'BGH')
insert into foo values ('ABC123' , 'BGH')
insert into foo values ('ABC123' , 'CDD')
insert into foo values ('ABCD123' , 'AFG')
insert into foo values ('ABCD123' , 'AFG')
insert into foo values ('ABCD123' , 'BGH')
insert into foo values ('ABCD123' , 'BGH')
insert into foo values ('ABCD123' , 'CDE')
select ArtNo, max(pri) pri
into #temp
from foo
group by ArtNo
update foo
set foo.pri = #temp.pri
from foo,#temp
where foo.Artno = #temp.ArtNo
select * from foo
drop table foo|||Hmm..
What if BGH has the highest priority?
The priority is not determined by the alphabet. :-(|||Then you will need a table or some other method to determine an ordered set of priorities.
As a start, look at how you do it now.|||I'm at home now, but something struck me on the way home...
If I change the three letter combinations to numbers according to their priority, I can then use:
SELECT articlenumber, MAX(tod)
FROM mytable
GROUP BY articlenumber
into a temptable..
The I use the temptable and updates ALL articlenumbers with their right tod.
I'll try it tomorrow. Clock is 5 in the afternoon in sweden now. :-)|||I'm at home now, but something struck me on the way home...
If I change the three letter combinations to numbers according to their priority, I can then use:
SELECT articlenumber, MAX(tod)
FROM mytable
GROUP BY articlenumber
into a temptable..
Then I use the temptable and updates ALL articlenumbers with their right tod.
I'll try it tomorrow. Clock is 5 in the afternoon in sweden now. :-)|||I'm at home now, but something struck me on the way home...
If I change the three letter combinations to numbers according to their priority, I can then use:
SELECT articlenumber, MAX(tod)
FROM mytable
GROUP BY articlenumber
into a temptable..
Then I use the temptable and updates ALL articlenumbers with their right tod.
I'll try it tomorrow. Clock is 5 in the afternoon in sweden now. :-)|||Worked like a charm.. My solution:
--
/* Get priorities of tod */
UPDATE mytable
SET tod =
CASE tod
WHEN 'BGH' THEN '1'
WHEN 'AFG' THEN '2'
WHEN 'CDD' THEN '3'
ELSE '999'
END
/* Create temptable */
create table dbo.temp_table (articlenumber varchar(31), PRIO varchar(10))
/* Make temporary list with "highest" TOD */
insert into dbo.temp_table (articlenumber, PRIO)
select articlenumber,
CASE MIN(cast(tod AS INT))
WHEN '1' THEN 'BGH'
WHEN '2' THEN 'AFG'
WHEN '3' THEN 'CDD'
ELSE 'N/A'
END
from mytable
GROUP BY articlenumber
/* Update ALL to highest tod */
UPDATE mytable
SET tod = b.PRIO
FROM mytable a, dbo.temp_table b
WHERE
(a.articlenumber = b.articlenumber)
--
can i use single function for a big project ??...
Hi,
I wnat to write a single function in code behind file for all DML operation, and i just want to pass query string in its parameter. but when ever my whole project will use this function to insert, update and delete. what will be the performance.
bool DMLoperation(string str){obj.conn =new SqlConnection(obj.connstring);obj.cmd =new SqlCommand(str,obj.conn);obj.conn.Open();try{if(obj.cmd.ExecuteNonQuery() > 0){obj.conn.Close();return true;}else{obj.conn.Close();return false;}}catch{obj.conn.Close();return false;}} I am totally confused about performance, is it good idea or worst ?.
Are you going to create a SQL manager for your project?
seems it's better for you to overload SQL object other then just pass-in the sql string and execute (just my opinion)
What i can only share is my company did the same things, ie. build a customized SQL manager for SQL execution,
although performace is a key issue, you should also consider other benfit that come from this design; such as you
can run extra SQL within each SQL call, such as SQL logging;
if you build the Manager in clean and well design, i believe performance is not a problem.
Hope this help
|||My dear, can you send some snippet of code, to use by overloading SQL object, It may be help me
Can I use my own Stored Procedure template with SQL2005?
When I go to create a new stored procedure, function, etc. I get a default template... is there anyway to replace this template with one of my own? One that will already have the important information pre-populated (the stuff I always end up having to type in, like company name, etc.).
Hi,
in SQL Server Management Studio, choose View -> Template Explorer. (Or Ctrl + Alt + T). There you can manage templates. Edit them whatsoever.
Saturday, February 25, 2012
Can I use if statement in a table valued function?
hi,
I am using a function in sql server 2005 like this:
...... myfunction(... @.FlagOn int)
.......
begin
return
(
if(@.FlagOn = 1)
select * from.......
else
select * form....
)
end
But it keeps complaining there is some syntax error around if. What is it?
Thanks.
No. Table valued function won’t allow this. Because it might cause more than one schema definition for the table valued function, which is not possible in the database.
If it is a filter then you can attach the additional condition on the where clause. If the flag is used to fetch different columns or different table create new function for each flag.
If the final schema is same (same number of columns and identical datatype for both the flags), use table valued function (not inline table valued function).
Code Snippet
Create function getvalues(@.flag as int)
Returns @.result table (id int, name varchar(100))
As
Begin
If @.flag=1
Insert into @.result
Select Top 10 id, name from sysobjects
Else
Insert into @.result
Select Top 10 id,name from syscolumns
return;
End
Go
Select * from getvalues(1)
go
Select * from getvalues(0)
|||Thanks lot, it works great.
Another thing is, can I create index on table @.result?
Coz I am thinking something like this:
select * from getvalues(1) as s where s.ID > 100
If it has index on ID, it might be faster.
|||Maximum you can add Primary Key/Unique key on the table variable. If your values are unique then you can go for it.
@.result table (id int primary key, name varchar(100))
Can I Use ADOMD or AMO In ASP.NET 2.0
Hi~
I know some function can be used in debug mode,like MsgBox()...,in ASP.NET 2.0.
But I don't know that Can ADOMD or AMO be used in ASP.NET 2.0.
Could someone tell me?
thanks~
Server: SQL Server 2005
OS: Windows 2003
Yes you can use both ADOMD and AMO from ASP.NET. The main "trick" is to get the code executing as a user that has the approriate rights to access the required SSAS objects. Setting up your application pool up to run under a domain account that is in appropriate SSAS roles is one way of getting this to work.Can I use a UDF in an IF statement?
Is it possible to use a user defined function in an IF statement? The UDF returns a scalar value. Like:
IF dbo.DoSomething (@.x)
The only way I can see to call a UDF is with
SELECT @.y = dbo.DoSomething (@.x)
Thank you!
YES but you have to compare its result with something or your function should return bit or integer value IF dbo.DoSomething (@.x)=1
Tuesday, February 14, 2012
Can I purchase off-the-shelf xps? (e.g., encrypt/decrypt function
We are looking for an xp that performs a simple encrypt/decrypt function
that complies with the Advanced Encryption Standard (AES). For example, you
pass it a value and it encrypts or decrypts the value.
Are there third party xps available? And if so, where might I find these xps?
Thanks
Dave
Here's one:
http://www.activecrypt.com/
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave
|||http://www.xpcrypt.com/
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave
|||If you can wait for the release of SQL2005 this is built in to the product.
HTH
Jasper Smith (SQL Server MVP)
http://www.sqldbatips.com
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave
Can I purchase off-the-shelf xps? (e.g., encrypt/decrypt function
We are looking for an xp that performs a simple encrypt/decrypt function
that complies with the Advanced Encryption Standard (AES). For example, you
pass it a value and it encrypts or decrypts the value.
Are there third party xps available? And if so, where might I find these xps?
Thanks
DaveHere's one:
http://www.activecrypt.com/
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave|||http://www.xpcrypt.com/
--
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave|||If you can wait for the release of SQL2005 this is built in to the product.
--
HTH
Jasper Smith (SQL Server MVP)
http://www.sqldbatips.com
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave|||I am looking at xp_crypt which I believe is the same as activecrypt.
This a full blown solution that automativcally creates views, triggers, etc.
to support the encryption function.
I was wondering if there might be a scalled down encryption solution; just a
dll that performs encrypt/decrypt and could be registered as an extended
stored procedure in SQL Server.
Does anyone know if something like this is available?
"Mike Epprecht (SQL MVP)" wrote:
> http://www.xpcrypt.com/
> --
> --
> Mike Epprecht, Microsoft SQL Server MVP
> Zurich, Switzerland
> IM: mike@.epprecht.net
> MVP Program: http://www.microsoft.com/mvp
> Blog: http://www.msmvps.com/epprecht/
> "Dave" <Dave@.discussions.microsoft.com> wrote in message
> news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> > Can anyone tell me if there is a market for extended stored procedures?
> >
> > We are looking for an xp that performs a simple encrypt/decrypt function
> > that complies with the Advanced Encryption Standard (AES). For example,
> > you
> > pass it a value and it encrypts or decrypts the value.
> >
> > Are there third party xps available? And if so, where might I find these
> > xps?
> >
> > Thanks
> > Dave
>
>
Can I purchase off-the-shelf xps? (e.g., encrypt/decrypt function
We are looking for an xp that performs a simple encrypt/decrypt function
that complies with the Advanced Encryption Standard (AES). For example, you
pass it a value and it encrypts or decrypts the value.
Are there third party xps available? And if so, where might I find these xps
?
Thanks
DaveHere's one:
http://www.activecrypt.com/
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave|||http://www.xpcrypt.com/
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave|||If you can wait for the release of SQL2005 this is built in to the product.
HTH
Jasper Smith (SQL Server MVP)
http://www.sqldbatips.com
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:76DCE302-380F-4646-AD41-54C8CB7A569D@.microsoft.com...
> Can anyone tell me if there is a market for extended stored procedures?
> We are looking for an xp that performs a simple encrypt/decrypt function
> that complies with the Advanced Encryption Standard (AES). For example,
> you
> pass it a value and it encrypts or decrypts the value.
> Are there third party xps available? And if so, where might I find these
> xps?
> Thanks
> Dave
Sunday, February 12, 2012
Can I populate an existing table from inline function table data?
d
from an inline function table. An external app uses the data from the inlin
e
function table in a form. I need to be able to update that data also in a
form, so I need to populate an existing table with the same data. Yes, I
could write an sp and some code, but I am just checking if there is a way to
grab the data from the inline function table to place in the existing table.
I tried Insert Into in the function - which it said is not allowed.
My issue is that the function takes a lot of arguments (10 args). From the
form, all I have to do is say
Frm.Recorsource = "Select * from dbo.f_data(" & strArgs & ")"
It would take way more code to do the same thing with an SP and 10 Params.
Is there a way to grab the inline function table data?
Thanks,
RichAre you saying that you cannot use an inline function as the source for a SE
LECT which is the source
for an INSERT? This is not what I am seeing:
USE tempdb
GO
CREATE TABLE a(c1 int, c2 int)
GO
CREATE FUNCTION f()
RETURNS TABLE
AS
RETURN(
SELECT 1 AS c1, 11 AS c2
UNION ALL
SELECT 2 AS c1, 22 AS c2
)
GO
INSERT INTO a(c1, c2)
SELECT c1, c2 FROM f()
GO
SELECT * FROM a
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Rich" <Rich@.discussions.microsoft.com> wrote in message
news:088A39C0-59BB-4037-81F8-F777F7C8FF90@.microsoft.com...
>I need to populate an existing table with the same data contained in/return
ed
> from an inline function table. An external app uses the data from the inl
ine
> function table in a form. I need to be able to update that data also in a
> form, so I need to populate an existing table with the same data. Yes, I
> could write an sp and some code, but I am just checking if there is a way
to
> grab the data from the inline function table to place in the existing tabl
e.
> I tried Insert Into in the function - which it said is not allowed.
> My issue is that the function takes a lot of arguments (10 args). From th
e
> form, all I have to do is say
> Frm.Recorsource = "Select * from dbo.f_data(" & strArgs & ")"
> It would take way more code to do the same thing with an SP and 10 Params.
> Is there a way to grab the inline function table data?
> Thanks,
> Rich