Showing posts with label t-sql. Show all posts
Showing posts with label t-sql. Show all posts

Thursday, March 22, 2012

Calling into a C# DLL from T-SQL

Hi:
I have an encryption DLL written in C#. Is there a way I can call into it
for encrypting and decrypting data during a T-SQL query?
Thanks,
CharlieSQL Server 2000? No; there is no supported way.
SQL Server 2005 -- yes. You can create a CLR UDF and wrap the DLL. But in
2005 it would make more sense to use the built-in encryption features that
SQL Server provides.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Charlie@.CBFC" <charle1@.comcast.net> wrote in message
news:%23MyZ0rt6FHA.2576@.TK2MSFTNGP09.phx.gbl...
> Hi:
> I have an encryption DLL written in C#. Is there a way I can call into it
> for encrypting and decrypting data during a T-SQL query?
> Thanks,
> Charlie
>|||Hello Adam,

> SQL Server 2000? No; there is no supported way.
Not that I like to disagree with Adam [ :) ] but... could you generate a
COM callable wrapper then write an XP that use that to do this work?
But I do agree, with SQL Server 2005, use the built in stuff instead!
Thank you,
Kent Tegels
DevelopMentor
http://staff.develop.com/ktegels/|||You -could-, but it is explicitly mentioned as not supported in BOL.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Kent Tegels" <ktegels@.develop.com> wrote in message
news:b87ad74d0fd8c7b8c31d3bd030@.news.microsoft.com...
> Hello Adam,
>
> Not that I like to disagree with Adam [ :) ] but... could you generate a
> COM callable wrapper then write an XP that use that to do this work?
> But I do agree, with SQL Server 2005, use the built in stuff instead!
> Thank you,
> Kent Tegels
> DevelopMentor
> http://staff.develop.com/ktegels/
>|||"Charlie@.CBFC" <charle1@.comcast.net> wrote in message
news:%23MyZ0rt6FHA.2576@.TK2MSFTNGP09.phx.gbl...
> Hi:
> I have an encryption DLL written in C#. Is there a way I can call into it
> for encrypting and decrypting data during a T-SQL query?
> Thanks,
> Charlie
>
There is in SQL2005, not in SQL2000|||... and here is my canned response to calling managed code from SQL Server
(not including Yukon,
which is of course a different story):
It is not supported for extended stored procedures or sp_OA procedures to ca
ll .NET code in CLR;
hosted within SQL Server's address space..
See:
http://support.microsoft.com/defaul...kb;en-us;322884
Also, below is with permission from David Browne, explaining how you can hav
e SQL Server execute CLR
code executing in its own process:
"
Short answer: Don't do it.
Calling managed code inside a stored procedure is not supported.
http://support.microsoft.com/defaul...kb;en-us;322884
At least not directly. You need some sort of unmanaged proxy to communicate
with your component running in another process.
For instance, http, or, drum roll, a COM+ Server Application.
This will cause COM+ to load an unmanaged proxy object in the SqlServer
process and will load the CLR into a COM+ surrogate process (dllhost.exe).
Which somebody here mentioned last w, and I just got around to testing.
It's all perfectly transparent to you, but you have to set up the COM+
server application.
Remember this is something different from .net remoting. With .NET remoting
you have a _managed_ proxy object in the local process, and so you load the
CLR in the local process as well as the remote process.
Anyway here's what I did:
I created this VB class
comTest.vb listing:
Imports System.Runtime.InteropServices
<ClassInterface(ClassInterfaceType.AutoDual),
ProgId("comTest.comTestClass")> _
Public Class comTest
Public Function Hello() As String
Return "hello"
End Function
End Class
build comTest.dll and registered it with
regasm /codebase comTest.dll /tlb:comTest.tlb
(complains that I haven't strong-named my assembly, which you should do.)
created an empty COM+ server application, set to run under a local
administrator account, and dragged comTest.dll into its components folder.
created an unmanaged host (vbscript will do), and invoked the component
using IDispach just like SQLServer.
test.vbs listing
Set d = CreateObject("comTest.comTestClass")
MsgBox d.Hello
Then I used the .net command line debugger cordbg.exe's 'pro' command to
list the processes hosting the CLR. And procexp.exe from
www.sysinternals.com to verify that the CLR's dll's were not loaded in my
unmanaged process. My unmanaged host did not load the CLR, although it
loaded "comsvcs.dll", and the CLR was loaded by the dllhost.exe process.
Then in sql I ran
declare @.object int
declare @.msg varchar(50)
declare @.rc int
declare @.hr int
declare @.source varchar(1000)
declare @.description varchar(1000)
exec @.rc = sp_oacreate 'comTest.comTestClass', @.object output
if @.rc <> 0
begin
EXEC @.hr = sp_OAGetErrorInfo @.object, @.source OUT, @.description OUT
print 'create failed ' + @.description
return
end
exec @.rc = sp_oamethod @.object, 'Hello', @.msg output
if @.rc <> 0
begin
EXEC @.hr = sp_OAGetErrorInfo @.object, @.source OUT, @.description OUT
print 'method failed ' + @.description
return
end
print 'return: ' + @.msg
exec @.rc = sp_oadestroy @.object
if @.rc <> 0
begin
EXEC @.hr = sp_OAGetErrorInfo @.object, @.source OUT, @.description OUT
print 'destroy failed ' + @.description
return
end
Ran fine, and still only one CLR loaded into dllhost.exe's process. So
think we can safely conclude that COM+ server applications do not violate
the prohibition against running managed code in SQLServer's process and
provide a convenient mechanism for interoperating with managed code from
TSQL.
David
"
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Adam Machanic" <amachanic@.hotmail._removetoemail_.com> wrote in message
news:OsmkX9t6FHA.636@.TK2MSFTNGP10.phx.gbl...
> You -could-, but it is explicitly mentioned as not supported in BOL.
> --
> Adam Machanic
> Pro SQL Server 2005, available now
> http://www.apress.com/book/bookDisplay.html?bID=457
> --
>
> "Kent Tegels" <ktegels@.develop.com> wrote in message
> news:b87ad74d0fd8c7b8c31d3bd030@.news.microsoft.com...
>

Monday, March 19, 2012

Calling All T-SQL Gods & Goddesses

Hi

I'm having trouble with the script below that it just won't do a
correct ORDER BY for a date field.

When executing the two SELECT TOP statements on their own the records
are sorted correctly but when wrapped in the main statement, the ORDER
BY is just ignored. Tried to play around with the script but with no
luck. Script is for paging, in this case, select all records meeting
criteria, order and select Top 25 to display on first page. Any
questions, please don't hesitate contacting me.

Thanks very much to anyone who's having a look at this!

Martin

SELECT COALESCE((i2b_contact.Firstname + CHAR(32) +
i2b_contact.Lastname),i2b_company.CompanyName) AS CName,
i2b_keytransactionlog.KeyTransactionLogID, i2b_key.KeyCode,
(i2b_address.Address1) AS PropertyAddress,
A.ProgUserName AS ProgUserName,
CONVERT (varchar(10), i2b_keytransactionlog.TransactionDate, 104 ) AS
TransactionDate,
CONVERT(varchar(10),i2b_keytransactionlog.ReturnBy Date,104) AS
ReturnByDate
FROM i2b_keytransactionlog
LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE KeyTransactionLogID IN(SELECT TOP 25 KeyTransactionLogID FROM
i2b_keytransactionlog
LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE i2b_keytransactionlog.ProgClientID = 1 AND
i2b_keytransactionlog.ProgUserID = 3 AND KeyTransactionLogID NOT IN
(SELECT TOP 0 KeyTransactionLogID FROM i2b_keytransactionlog
LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE i2b_keytransactionlog.ProgClientID = 1 AND
i2b_keytransactionlog.ProgUserID = 3 ORDER BY ReturnByDate DESC)
ORDER BY ReturnByDate DESC)
ORDER BY ReturnByDate DESCOn 29 May 2004 12:19:20 -0700, Martin wrote:

>Hi
>I'm having trouble with the script below that it just won't do a
>correct ORDER BY for a date field.
>When executing the two SELECT TOP statements on their own the records
>are sorted correctly but when wrapped in the main statement, the ORDER
>BY is just ignored. Tried to play around with the script but with no
>luck. Script is for paging, in this case, select all records meeting
>criteria, order and select Top 25 to display on first page. Any
>questions, please don't hesitate contacting me.
>Thanks very much to anyone who's having a look at this!
>Martin
>SELECT COALESCE((i2b_contact.Firstname + CHAR(32) +
>i2b_contact.Lastname),i2b_company.CompanyName) AS CName,
>i2b_keytransactionlog.KeyTransactionLogID, i2b_key.KeyCode,
>(i2b_address.Address1) AS PropertyAddress,
>A.ProgUserName AS ProgUserName,
>CONVERT (varchar(10), i2b_keytransactionlog.TransactionDate, 104 ) AS
>TransactionDate,
>CONVERT(varchar(10),i2b_keytransactionlog.ReturnBy Date,104) AS
>ReturnByDate
> FROM i2b_keytransactionlog
>LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
>i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
>LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
>i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
>JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
>JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
>JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
>JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
>A.ProgUserID)
>WHERE KeyTransactionLogID IN(SELECT TOP 25 KeyTransactionLogID FROM
>i2b_keytransactionlog
>LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
>i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
>LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
>i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
>JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
>JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
>JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
>JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
>A.ProgUserID)
>WHERE i2b_keytransactionlog.ProgClientID = 1 AND
>i2b_keytransactionlog.ProgUserID = 3 AND KeyTransactionLogID NOT IN
> (SELECT TOP 0 KeyTransactionLogID FROM i2b_keytransactionlog
>LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
>i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
>LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
>i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
>JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
>JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
>JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
>JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
>A.ProgUserID)
>WHERE i2b_keytransactionlog.ProgClientID = 1 AND
>i2b_keytransactionlog.ProgUserID = 3 ORDER BY ReturnByDate DESC)
> ORDER BY ReturnByDate DESC)
> ORDER BY ReturnByDate DESC

Hi Martin,

I'm not entirely sure why you would use TOP 0 for the NOT IN subquery, but
that's not the cause of the problem you post about.

The ANSI standard for SQL allows only columns appearing in the SELECT list
in the ORDER BY clause. The rationale being that first, the query gets
executed and the result set is determined; the sorting is a seperate,
final stage. ANSI SQL allows things as ORDER BY 3 (3 being the third
column in the SELECT LIST) or SELECT MyColumn AS ColumnAlias ... ORDER BY
ColumnAlias.

MS SQL Server expands on this standard. With MS SQL Server, you can ORDER
BY on a column not in the SELECT list. If you use a column name that is
not an alias in the column list, SQL Server will try to locate a column
with that name in one of the tables used in the query and use that for
sorting.

Before I get back to your problem, run the following script. It will
clearly show that SQL Server will first attempt to resolve names in the
ORDER BY clause following ANSI standard before looking at the columns that
make up the data.

CREATE TABLE testtab (a int, b int)
go
INSERT testtab (a, b) SELECT 1, 20
INSERT testtab (a, b) SELECT 2, 10
go
SELECT b
FROM testtab
ORDER BY a
go
SELECT b AS a
FROM testtab
ORDER BY a
go
SELECT b AS a, a AS b
FROM testtab
ORDER BY a
go
DROP TABLE testtab
go

In the first query, no column "a" is in the SELECT list, so SQL Server
will sort on the column a in the table. In the second query, the SELECT
list has a column aliased as "a", so this is used for sorting. In the
third query, the column a itself is used in the select list; aliases are
disregarded and sorting is done on the contents of a.

Now back to your real problem. I'll show you the parts that cause the
unexpected ordering:

>SELECT (...)
> (...)
>CONVERT(varchar(10),i2b_keytransactionlog.ReturnBy Date,104) AS
>ReturnByDate
> (...)
> ORDER BY ReturnByDate DESC

The column ReturnByDate is not used in the SELECT list (the results of a
function are not cosidered equal to the column itself). There is however
an alias ReturnByDate, so there's no need to get back to the data in the
table either - the aliased column in the result set is used. This column
is the output of a CONVERT function call. It's varchar(10), therefor the
sorting will be done by alphabet, descending. "23-04-2003" will go before
"06-05-2004".

The fix? Either use something else as an alias ([Return By Date], for
example) or use ORDER BY CONVERT(datetime, ReturnByDate).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Sunday, March 11, 2012

Calling a SQL script from T-SQL code

Hi !
I am using Win2K and Microsoft SQL Server 2000.
I have a text file of more than 10000 lines of T-SQL.
Depending on some condition to be determined in another SQL script at run
time,
I should like to have the T-SQL in that text file executed or not.
Can I have a SQL script executed from within another SQL script ?
Thanks for any helpful remarks.
PeterWhy can't you put the script into a proc and then execute the proc
conditionally?
I guess you could make a call to OSQL via xp_cmdshell if you must
execute a script file. I can't think of a good reason to do it that way
but I can think of plenty of good reasons why you *shouldn't* do it
that way!
David Portas
SQL Server MVP
--|||peshrad wrote:
> Hi !
> I am using Win2K and Microsoft SQL Server 2000.
> I have a text file of more than 10000 lines of T-SQL.
> Depending on some condition to be determined in another SQL script at run
> time,
> I should like to have the T-SQL in that text file executed or not.
> Can I have a SQL script executed from within another SQL script ?
> Thanks for any helpful remarks.
> Peter
>
you can create a job with first step to check condition and second to
run the script (if the condition is met)

Wednesday, March 7, 2012

Call Oracle procedure using T-SQL (e.g. from sql server stored procedure)

I am writing a Sql Server stored procedure that performs several operations against an Oracle database. Some of these operations could be aggregated into a single Oracle stored procedure, which I have created.

My question is, how can I call the Oracle stored procedure from my Sql Server stored procedure? I have a linked server up and working, but the execute syntax I have tried just doesn't work. I have also tried using the "CALL" command, with no success.

Has anyone done this without resorting to an extended stored procedure? Examples are appreciated!In answer to my own question, in books online I found the following statement under the topic "External Data and Transact-SQL":

"Stored procedures are supported only against SQL Server data sources."

Ah well, at least I was able to get functions w/input parameters to work...|||Hey...I also have the same issue to be resolved.
Let me know if u are able to solve that...

Cheers !!!
~ HK

Saturday, February 25, 2012

call a sap remote-function-module from t-sql Stored-Proedure

Hello,

is it possible to call an sap remote-function-module directly from a stored-procedure in SQL-Server-2005?

If yes, where can I find an example?

Thanks

Gerd

Hi,

Never touched SAP, but can you run the remote function module from a DOS prompt? if so, could you use xp_cmdshell from the extended stored procedures?

Just an idea :)

Cheers,
Menthos

Sunday, February 12, 2012

Calculating Age...

Hello..
I am attempting to write a T-SQL script that will calculate a person's age
based on a date in the DOB field. The DOB field is a smalldatetime. I want
to return this calculated field in a result set. So for example, if the
date in the DOB field is 01/01/1900... I want the age to be returned as 105
(2005 - 1900). I am using SQL Server 2000
Any help would be appreciated...
Thanks,
Bretthttp://www.aspfaq.com/2233
This is my signature. It is a general reminder.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"Brett Davis" <bdavis123@.cox.net> wrote in message
news:OTREukdQFHA.2932@.TK2MSFTNGP09.phx.gbl...
> Hello..
> I am attempting to write a T-SQL script that will calculate a person's age
> based on a date in the DOB field. The DOB field is a smalldatetime. I
> want to return this calculated field in a result set. So for example, if
> the date in the DOB field is 01/01/1900... I want the age to be returned
> as 105 (2005 - 1900). I am using SQL Server 2000
> Any help would be appreciated...
> Thanks,
> Brett
>
>|||So say that this date is the birth date:
DROP FUNCTION TIMEMEMORIAL
GO
CREATE FUNCTION TIMEMEMORIAL
(
@.BIRTHDATE smalldatetime,
@.CURRENTDATE smalldatetime
)
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @.CURRENTYEAR CHAR(4)
DECLARE @.BIRTHYEAR CHAR(4)
DECLARE @.Result varchar(200)
SET @.BIRTHYEAR = CAST(YEAR(@.BIRTHDATE) as Char(4))
SET @.CURRENTYEAR = CAST(YEAR(@.CURRENTDATE) as Char(4))
Select @.result = CAST(datediff(yy,@.BIRTHDATE,@.CURRENTDATE
) AS varchar(10)) +
' (' + @.BIRTHYEAR + ' - ' + @.CURRENTYEAR + ')'
RETURN @.Result
END
Select dbo.TimeMemorial('20000101',getdate())
HTH, Jens Smeyer
http://www.sqlserver2005.de
--
"Brett Davis" <bdavis123@.cox.net> schrieb im Newsbeitrag
news:OTREukdQFHA.2932@.TK2MSFTNGP09.phx.gbl...
> Hello..
> I am attempting to write a T-SQL script that will calculate a person's age
> based on a date in the DOB field. The DOB field is a smalldatetime. I
> want to return this calculated field in a result set. So for example, if
> the date in the DOB field is 01/01/1900... I want the age to be returned
> as 105 (2005 - 1900). I am using SQL Server 2000
> Any help would be appreciated...
> Thanks,
> Brett
>
>|||See :
http://groups.google.ca/groups?selm...FTNGP09.phx.gbl
Anith|||I think this is easier;
checking for VALID data...
Making assumption that your want it from TODAY
select
case
when ISDATE(convert(char(15),[DATEFIELD])) = 0 then null
else
datediff(year, convert(datetime,([DATEFIELD])),getdate(
))
end
"Brett Davis" wrote:

> Hello..
> I am attempting to write a T-SQL script that will calculate a person's age
> based on a date in the DOB field. The DOB field is a smalldatetime. I wa
nt
> to return this calculated field in a result set. So for example, if the
> date in the DOB field is 01/01/1900... I want the age to be returned as 10
5
> (2005 - 1900). I am using SQL Server 2000
> Any help would be appreciated...
> Thanks,
> Brett
>
>
>|||On Fri, 15 Apr 2005 10:29:04 -0700, Jim Yurt wrote:

>I think this is easier;
>checking for VALID data...
>Making assumption that your want it from TODAY
>select
>case
>when ISDATE(convert(char(15),[DATEFIELD])) = 0 then null
>else
>datediff(year, convert(datetime,([DATEFIELD])),getdate(
))
>
>end
Hi Jim,
But that will return the number of year boundaries (dec 31 / Jan 1)
since the birth date, not the age!
SELECT DATEDIFF(year, '20041231', '20050101'),
DATEDIFF(year, '20040101', '20051231')
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Try This:
If @.DOB is datetime date of birth, and
@.CurDT is a dateTime with current date, then
Select Year(@.CurDT) - Year(@.DOB) -
Case When Month(@.CurDT) < Month(@.DOB) Then 1
When Month(@.CurDT) > Month(@.DOB) Then 0
When Day(@.CurDT) < Day(@.DOB) Then 1
Else 0 End
"Brett Davis" wrote:

> Hello..
> I am attempting to write a T-SQL script that will calculate a person's age
> based on a date in the DOB field. The DOB field is a smalldatetime. I wa
nt
> to return this calculated field in a result set. So for example, if the
> date in the DOB field is 01/01/1900... I want the age to be returned as 10
5
> (2005 - 1900). I am using SQL Server 2000
> Any help would be appreciated...
> Thanks,
> Brett
>
>
>