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

Friday, March 23, 2012

Is it possible to enable TCP/IP connection to SQL Server programmatically? Is there any function

Anyone know? I'm making an installation package and I want my installation program (which is a custom action class inherited from Installer Class) to adjust the network configuration of SQL Server, for example enable TCP/IP. What can I do? Is that any function in SMO package that can do this for me?Hi,

that is a registry setting, you have to switch the appropiate network protocol values in this section to get it working:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer\SuperSocketNetLib

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

Wednesday, March 21, 2012

Is it possible to create CLR function call legacy C++ models?

I tried to convert an legacy C++ program to managed C++ and build a CLR
function. However, too much compiler error. Is it possible to build a CLR
function and call/link.. the legacy C++ code?examnotes <nick@.discussions.microsoft.com> wrote in
news:DA2E1EC7-EFDB-4111-B3A9-0314A8FAA394@.microsoft.com:

> I tried to convert an legacy C++ program to managed C++ and build a
> CLR function. However, too much compiler error. Is it possible to
> build a CLR function and call/link.. the legacy C++ code?
>
As (I believe) I replied earlier to you, you need to either access the C++
object through P/Invoke or COM Interop. In your case it sounds like
P/Invoke is the most likely way of getting to the code, i.e. your C++
object is not a COM component.
Niels
****************************************
**********
* Niels Berglund
* http://staff.develop.com/nielsb
* nielsb at develop dot com
* "A First Look at SQL Server 2005 for Developers"
* http://www.awprofessional.com/title/0321180593
****************************************
**********|||Thanks. Sorry for repost.
One thing I want to know is,
It sounds CLR function required /clr:safe, can safe code still do platform
invoke?
"Niels Berglund" wrote:

> examnotes <nick@.discussions.microsoft.com> wrote in
> news:DA2E1EC7-EFDB-4111-B3A9-0314A8FAA394@.microsoft.com:
>
> As (I believe) I replied earlier to you, you need to either access the C++
> object through P/Invoke or COM Interop. In your case it sounds like
> P/Invoke is the most likely way of getting to the code, i.e. your C++
> object is not a COM component.
> Niels
>
> --
> ****************************************
**********
> * Niels Berglund
> * http://staff.develop.com/nielsb
> * nielsb at develop dot com
> * "A First Look at SQL Server 2005 for Developers"
> * http://www.awprofessional.com/title/0321180593
> ****************************************
**********
>|||examnotes <nick@.discussions.microsoft.com> wrote in
news:7205E76E-7B6D-4572-B5EF-B01A7E312456@.microsoft.com:

> It sounds CLR function required /clr:safe, can safe code still do
> platform invoke?
>
In order to do P/Invoke you need to create your assembly in SQL as unsafe.
Niels
****************************************
**********
* Niels Berglund
* http://staff.develop.com/nielsb
* nielsb at develop dot com
* "A First Look at SQL Server 2005 for Developers"
* http://www.awprofessional.com/title/0321180593
****************************************
**********|||"Niels Berglund" <nielsb@.nospam.develop.com> wrote in message
news:Xns97AADD8FEE8DEnielsbdevelopcom@.20
7.46.248.16...
> examnotes <nick@.discussions.microsoft.com> wrote in
> news:7205E76E-7B6D-4572-B5EF-B01A7E312456@.microsoft.com:
>
> In order to do P/Invoke you need to create your assembly in SQL as unsafe.
>
Running unsafe code in the database is, well, unsafe. Your legacy C++ code
can crash SQL Server. So you should check with whoever owns the production
SQL Server you will target and make sure they are OK with you running this
code inside the database.
David|||Organization: DevelopMentor
Message-ID: <Xns97AB12F509BB6nielsbdevelopcom@.207.46.248.16>
User-Agent: Xnews/2006.03.14
Newsgroups: microsoft.public.sqlserver.programming
Date: Wed, 19 Apr 2006 17:51:47 -0700
NNTP-Posting-Host: 69.177.80.118.adsl.snet.net 69.177.80.118
Path: TK2MSFTNGP01.phx.gbl!TK2MSFTNGP04.phx.gbl
Lines: 1
Xref: TK2MSFTNGP01.phx.gbl microsoft.public.sqlserver.programming:597705
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
news:ufkHThAZGHA.4836@.TK2MSFTNGP05.phx.gbl:

> Running unsafe code in the database is, well, unsafe. Your legacy C++
> code can crash SQL Server. So you should check with whoever owns the
> production SQL Server you will target and make sure they are OK with
> you running this code inside the database.
>
Well, to put things into perspective; running unsafe CLR code inside the
database is inherently safer than running extended procs. But yes, you are
right in that the code should be checked so it won't cause damage.
Niels
****************************************
**********
* Niels Berglund
* http://staff.develop.com/nielsb
* nielsb at develop dot com
* "A First Look at SQL Server 2005 for Developers"
* http://www.awprofessional.com/title/0321180593
****************************************
**********|||"Niels Berglund" <nielsb@.nospam.develop.com> wrote in message
news:Xns97AB12F509BB6nielsbdevelopcom@.20
7.46.248.16...
> "David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
> news:ufkHThAZGHA.4836@.TK2MSFTNGP05.phx.gbl:
>
> Well, to put things into perspective; running unsafe CLR code inside the
> database is inherently safer than running extended procs. But yes, you are
> right in that the code should be checked so it won't cause damage.
>
Unsafe managed code is one thing. P/Invoke to legacy C++ code is another.
David|||well, clr function call legacy dll has too much trouble,
- need to set security to let the user has permission
- or create public key and use it to sign the assembly
- parameter passing...
make the solution less favorite.
i will go to bcp to text file, processing and bulk insert back
"David Browne" wrote:

> "Niels Berglund" <nielsb@.nospam.develop.com> wrote in message
> news:Xns97AB12F509BB6nielsbdevelopcom@.20
7.46.248.16...
> Unsafe managed code is one thing. P/Invoke to legacy C++ code is another.
> David
>
>sql

Is it possible to create an IIF function for SQL Server?

Hello!

I tried the following code:

create function dbo.iif
(
@.Expression bit,
@.TruePart sql_variant,
@.FalsePart sql_variant
)
returns sql_variant
as
begin
declare @.ReturnValue sql_variant

if @.Expression=1
begin
set @.ReturnValue=@.TruePart
end
else
begin
set @.ReturnValue=@.FalsePart
end

return @.ReturnValue
end

It works fine with statements like this:
select dbo.iif(1,'True','False')

However, when trying a "real" expression, an error appears:
select dbo.iif((1=0),'True','False')
Line 1: Incorrect syntax near '='.

How can I work around this?

Thank you very much in advance.define a variant, and set the value to be the expression. Use the variant in your iif function instead.|||would CASE serve the purpose?

Is it possible to create a UDF from a .NET 2.0 assembly that uses dllimport to call unmanaged co

I have been reading up on everything I can find on this subject and I am not clear if this is allowed within the SQL Server 2005 CLR. My function calls work within a Windows form project, but don't run when invoked from a SQL function. I don't get any errors or warnings when creating the assembly and functions within SQL Server, but when running via a select statement, the spid just hangs and I have to stop & restart the service to kill the process. I have been investigating the security settings for this assembly, but I think I have that covered via the RunTime Security Policy settings in the .NET Framework 2.0 Configuration tool.

Any insights, knowledge, or thoughts would be greatly appreciated.

Barry

Hi Barry,

Calling into a CLR function which in turn p-invokes into unmanaged code should work without issues, although we would usually recommend against this if you can avoid it, as it would require the assembly being registered as UNSAFE.

What is the unmanaged code doing? Is it possible to have a look at your SQL function?

Thanks!

-Mat

sql

Is it possible to create a function from within a stored procedure?

I would like to create an UDF from withing a stored procedure. Is it possible?
Thanks a lot.Originally posted by EMoscosoCam
I would like to create an UDF from withing a stored procedure. Is it possible?

Thanks a lot.
try something like this:

declare @.S varchar(1000)

select @.S='
CREATE FUNCTION dbo.fn_test
(
@.p1 int,
@.P2 int
)
RETURNS int
AS
BEGIN
DECLARE @.sum AS int
SELECT @.sum = @.p1 + @.P2
RETURN @.sum
END'
exec(@.S)
go
select dbo.fn_test(5,2)|||Thank you very much! it worked just fine.

Is it possible to create a custom SQL session function/variable

To avoid having to pass this user specific ID via a parameter in the stored
procedure, is it possible to create your own function like (like
suser_sid()) or variable (like @.@.SPID) ?
This value would be passed from a Web Service (asp.net 2.0) in the
connection string or something and could be referred to anywhere in the SQL
code (SQL 2005).
Has anyone got a better solution than a SP parameter ?Take a look at CONTEXT_INFO in the Books Online. This allows you to store
up to 128 bytes of binary info that can be used anywhere in the session.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"GMG" <gmgsoftware@.nospam.net.au> wrote in message
news:uI04I9duHHA.4440@.TK2MSFTNGP06.phx.gbl...
> To avoid having to pass this user specific ID via a parameter in the
> stored
> procedure, is it possible to create your own function like (like
> suser_sid()) or variable (like @.@.SPID) ?
> This value would be passed from a Web Service (asp.net 2.0) in the
> connection string or something and could be referred to anywhere in the
> SQL
> code (SQL 2005).
> Has anyone got a better solution than a SP parameter ?
>

Is it possible to create a custom SQL session function/variable

To avoid having to pass this user specific ID via a parameter in the stored
procedure, is it possible to create your own function like (like
suser_sid()) or variable (like @.@.SPID) ?
This value would be passed from a Web Service (asp.net 2.0) in the
connection string or something and could be referred to anywhere in the SQL
code (SQL 2005).
Has anyone got a better solution than a SP parameter ?Take a look at CONTEXT_INFO in the Books Online. This allows you to store
up to 128 bytes of binary info that can be used anywhere in the session.
Hope this helps.
Dan Guzman
SQL Server MVP
"GMG" <gmgsoftware@.nospam.net.au> wrote in message
news:uI04I9duHHA.4440@.TK2MSFTNGP06.phx.gbl...
> To avoid having to pass this user specific ID via a parameter in the
> stored
> procedure, is it possible to create your own function like (like
> suser_sid()) or variable (like @.@.SPID) ?
> This value would be passed from a Web Service (asp.net 2.0) in the
> connection string or something and could be referred to anywhere in the
> SQL
> code (SQL 2005).
> Has anyone got a better solution than a SP parameter ?
>sql

Is it possible to create a custom SQL session function/variable

To avoid having to pass this user specific ID via a parameter in the stored
procedure, is it possible to create your own function like (like
suser_sid()) or variable (like @.@.SPID) ?
This value would be passed from a Web Service (asp.net 2.0) in the
connection string or something and could be referred to anywhere in the SQL
code (SQL 2005).
Has anyone got a better solution than a SP parameter ?Asked and answered in .programming. Please refrain from multiposting.
Adam Machanic
SQL Server MVP
Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220
"GMG" <gmgsoftware@.nospam.net.au> wrote in message
news:%23cNc2ceuHHA.3376@.TK2MSFTNGP04.phx.gbl...
> To avoid having to pass this user specific ID via a parameter in the
> stored
> procedure, is it possible to create your own function like (like
> suser_sid()) or variable (like @.@.SPID) ?
> This value would be passed from a Web Service (asp.net 2.0) in the
> connection string or something and could be referred to anywhere in the
> SQL
> code (SQL 2005).
> Has anyone got a better solution than a SP parameter ?
>|||Take a look at SET CONTEXT_INFO in the Books Online. That allows you to set
a value for the connection that is accessible anywhere in the session with
the CONTEXT_INFO function. Since the value is binary, you'll need to
convert as needed.
Hope this helps.
Dan Guzman
SQL Server MVP
"GMG" <gmgsoftware@.nospam.net.au> wrote in message
news:%23cNc2ceuHHA.3376@.TK2MSFTNGP04.phx.gbl...
> To avoid having to pass this user specific ID via a parameter in the
> stored
> procedure, is it possible to create your own function like (like
> suser_sid()) or variable (like @.@.SPID) ?
> This value would be passed from a Web Service (asp.net 2.0) in the
> connection string or something and could be referred to anywhere in the
> SQL
> code (SQL 2005).
> Has anyone got a better solution than a SP parameter ?
>

Monday, March 19, 2012

Is it possible to change tempdb?

Hi,
Is it possible to define another database to function as the tempdb instead
of the default tempdb?
I'm using sql2000
Thanks,
I.P.hi I.P.,
"I.P." <lafafa@.yahoo.com> ha scritto nel messaggio
news:cr8l1d$84e$1@.news.iucc.ac.il
> Hi,
> Is it possible to define another database to function as the tempdb
> instead of the default tempdb?
> I'm using sql2000
>
no, it's not possible unless you reinstall the SQL Server 2000 instance, as
tmpdb collation is inherited from the system databases setting and is the
same as master...
you can not define an "alternative" user defined tempdb database...
but, as SQL Server 2000 supports multiple collations both at database level
and column level, you should not have this issue...
why do you need a different collation?
--
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply|||Andrea,
Thanks for the answer.
I need a different collation as my sql setup is Latin1 and my specific DB is
Hebrew. While using temp tables in my DB I have a problem as the tempdb
doesn't understand Hebrew... Is there a solution for such problem?
Thanks,
I.P.
"Andrea Montanari" <andrea.sqlDMO@.virgilio.it> wrote in message
news:33q6rrF41pmbiU1@.individual.net...
> hi I.P.,
> "I.P." <lafafa@.yahoo.com> ha scritto nel messaggio
> news:cr8l1d$84e$1@.news.iucc.ac.il
>> Hi,
>> Is it possible to define another database to function as the tempdb
>> instead of the default tempdb?
>> I'm using sql2000
> no, it's not possible unless you reinstall the SQL Server 2000 instance,
> as
> tmpdb collation is inherited from the system databases setting and is the
> same as master...
> you can not define an "alternative" user defined tempdb database...
> but, as SQL Server 2000 supports multiple collations both at database
> level
> and column level, you should not have this issue...
> why do you need a different collation?
> --
> Andrea Montanari (Microsoft MVP - SQL Server)
> http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
> DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
> (my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
> interface)
> -- remove DMO to reply
>|||We have a similar issue on some systems where the collation at installation
has been incorrectly set relative to user databases, causing join errors
with temp tables. You can rebuild the system databases, as the other posters
point out, or when the temp tables are created, you can specify an explicit
column-level collation, thus overriding the tempdb default.
Rgds,
Paul Ibison|||In addition, rather than specifiying a specific collation you can specify
database_default as the collation for columns in a temp table and they will
inherit the current database collation rather than using that if tempdb
--
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
"Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
news:Oi8B48M8EHA.2572@.tk2msftngp13.phx.gbl...
> We have a similar issue on some systems where the collation at
> installation
> has been incorrectly set relative to user databases, causing join errors
> with temp tables. You can rebuild the system databases, as the other
> posters
> point out, or when the temp tables are created, you can specify an
> explicit
> column-level collation, thus overriding the tempdb default.
> Rgds,
> Paul Ibison
>|||"Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
news:Oi8B48M8EHA.2572@.tk2msftngp13.phx.gbl...
> We have a similar issue on some systems where the collation at
> installation
> has been incorrectly set relative to user databases, causing join errors
> with temp tables. You can rebuild the system databases, as the other
> posters
> point out, or when the temp tables are created, you can specify an
> explicit
> column-level collation, thus overriding the tempdb default.
> Rgds,
> Paul Ibison
>

Is it possible to change tempdb?

Hi,
Is it possible to define another database to function as the tempdb instead
of the default tempdb?
I'm using sql2000
Thanks,
I.P.hi I.P.,
"I.P." <lafafa@.yahoo.com> ha scritto nel messaggio
news:cr8l1d$84e$1@.news.iucc.ac.il
> Hi,
> Is it possible to define another database to function as the tempdb
> instead of the default tempdb?
> I'm using sql2000
>
no, it's not possible unless you reinstall the SQL Server 2000 instance, as
tmpdb collation is inherited from the system databases setting and is the
same as master...
you can not define an "alternative" user defined tempdb database...
but, as SQL Server 2000 supports multiple collations both at database level
and column level, you should not have this issue...
why do you need a different collation?
--
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply

Is it possible to change tempdb?

Hi,
Is it possible to define another database to function as the tempdb instead
of the default tempdb?
I'm using sql2000
Thanks,
I.P.
hi I.P.,
"I.P." <lafafa@.yahoo.com> ha scritto nel messaggio
news:cr8l1d$84e$1@.news.iucc.ac.il
> Hi,
> Is it possible to define another database to function as the tempdb
> instead of the default tempdb?
> I'm using sql2000
>
no, it's not possible unless you reinstall the SQL Server 2000 instance, as
tmpdb collation is inherited from the system databases setting and is the
same as master...
you can not define an "alternative" user defined tempdb database...
but, as SQL Server 2000 supports multiple collations both at database level
and column level, you should not have this issue...
why do you need a different collation?
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply

Friday, March 9, 2012

Is it not possible to use UPDATE within a function?

Is it not possible to use UPDATE within a function? I get the following err
or message for the following function:
Evan
- - - - - - - - - - - - - -
Server: Msg 443, Level 16, State 2, Procedure fn_GetNextDataVersion, Line 10
Invalid use of 'UPDATE' within a function.
CREATE FUNCTION dbo.fn_GetNextDataVersion(@.tb_pk int)
RETURNS bigint
AS
BEGIN
DECLARE @.nextDataVersion bigint
SET @.nextDataVersion = 1 + (SELECT DataVersion FROM tb_TableList WHERE tb_pk
= @.tb_pk)
UPDATE tb_TableList SET DataVersion = @.nextDataVersion WHERE tb_pk = @.tb_pk
RETURN @.nextDataVersion
ENDDDL statements inside user-defined functions are only allowed on table
variables local to the function.
http://msdn.microsoft.com/library/d...>
_08_460j.asp
Objects cannot be created, altered or dropped if they exist outside the
scope of the user-defined function.
ML
http://milambda.blogspot.com/|||Table variables IF a table is created in that function?
"ML" <ML@.discussions.microsoft.com> wrote in message
news:B2FA2009-C30A-4DF4-B8CE-DC110EA8F77A@.microsoft.com...
> DDL statements inside user-defined functions are only allowed on table
> variables local to the function.
> http://msdn.microsoft.com/library/d...
es_08_460j.asp
> Objects cannot be created, altered or dropped if they exist outside the
> scope of the user-defined function.
>
> ML
> --
> http://milambda.blogspot.com/|||Yes. Locally. Variables are always local in SQL.
("This is a local shop. For local people." -- from 'A League of Gentlemen'
(BBC))
What is your goal exactly - there might be another way. If you share the
goal. :)
ML
http://milambda.blogspot.com/|||Create a function which gets a counter from a table and return the counter
WHILE updating the counter table to PLUS ONE
Evan
"ML" <ML@.discussions.microsoft.com> wrote in message
news:854D1DA1-3C1C-45B7-8A9B-E787B3856E55@.microsoft.com...
> Yes. Locally. Variables are always local in SQL.
>
> ("This is a local shop. For local people." -- from 'A League of Gentlemen'
> (BBC))
>
> What is your goal exactly - there might be another way. If you share the
> goal. :)
>
> ML
> --
> http://milambda.blogspot.com/|||for now i am working with a stored proc but a function is more elegant|||If you want to do it in a single step, then the procedure is the way to go.
ML
http://milambda.blogspot.com/

Is it necessary to order again?

I have a function that returns a table:

CREATE FUNCTION dbo.Example(@.Param int)
RETURNS @.Tbl TABLE (
Field1 int,
Field2 int) AS
BEGIN
INSERT @.Tbl (Field1,Field2)
SELECT FieldA,FieldB FROM DataTable
WHERE FieldC = @.Param
ORDER BY FieldA
RETURN
END

The statement that populates the table orders the data. In order
to ensure the results are ordered that way, should the call to the
function include an ordering? I.e., is this sufficient

SELECT * FROM dbo.Example(17)

or is this necessary? --

SELECT * FROM dbo.Example(17) ORDER BY Field1

Thanks!"Jim Geissman" <jim_geissman@.countrywide.com> wrote in message
news:b84bf9dc.0408051457.6ae418c0@.posting.google.c om...
> I have a function that returns a table:
> CREATE FUNCTION dbo.Example(@.Param int)
> RETURNS @.Tbl TABLE (
> Field1 int,
> Field2 int) AS
> BEGIN
> INSERT @.Tbl (Field1,Field2)
> SELECT FieldA,FieldB FROM DataTable
> WHERE FieldC = @.Param
> ORDER BY FieldA
> RETURN
> END
> The statement that populates the table orders the data.

No, it doesn't. Tables are sets of data. They have no order.

Now, your statement may put the data into the table in order... but there's
no guarantee that SQL Server will store it in that order.

> In order
> to ensure the results are ordered that way, should the call to the
> function include an ordering? I.e., is this sufficient
> SELECT * FROM dbo.Example(17)
No

> or is this necessary? --
> SELECT * FROM dbo.Example(17) ORDER BY Field1

Yes.

> Thanks!