Showing posts with label related. Show all posts
Showing posts with label related. Show all posts

Friday, March 23, 2012

Excuting SQL in a stored procedure

I know that you can execute a stored proc with the "execute" command, but is there any way to execute a string that is an sql string?

On a related note, is there a better way than building a string like "select * from table where" and then tacking on "value="+@.somevalue to do a search if your search is based on multiple (possibly null) variables? If you can answer the first question, dont bother with the second. =P

Its called dynamic SQL (or D-SQL).

declare @.sql varchar(max)

SET @.sql = 'SELECT * FROM TABLE WHERE NAME LIKE @.searchstring ORDER BY NAME' [or something like that]

EXEC sp_executesql @.sql, N'@.searchstring varchar(50)', @.searchstring

Now you have to be carefule about SQL Injection, which means that someone out there may try to insert SQL of their own like -- or WHERE 1=1 or other things that can force errors in your D-SQL, which report errors to the user which include things like table and column names. Which they can the use to get to your security tables or what have you. It can be very dangerous.

http://www.sommarskog.se/dynamic_sql.html

|||

Dynamic sql can be executed with thesp_executesql command however, if you know what possible columns the user could search on then it's best to use a CASE statement and avoid dynamic sql. If multiple columns may or not be searched, you can also useoptional parameters.

|||

There was a really ugly thread on that a few days ago and in the end the guy used D-SQL. Do you have a functioning example?

|||

I've never encountered a situation where I've needed to use D-SQL. Just start with the idea that you CANNOT use D-SQL. Like was said above, you can find a way using CASE statements in most cases. Why don't you post the stored procedure and maybe people can help you so you don't need to use D-SQL?

|||

Its not mine.

http://forums.asp.net/t/1163791.aspx

I think that there is a time and place for D-SQL. If you have a system that stores credit card numbers and stuff like that, well, yeah, no way. But if you don't have sensitive information in your DB, or if you can isolate your sensitive information on a seperate DB and maybe do joins through objects, D-SQL is safe enough. Starting with the idea that you can't use D-SQL no matter what is similar to a physician being told he can't use a particular medicine because some folks have died from overdoses of it.

I work for a gov't contractor on secure systems, and most of our work is framework development, so I haven't had to deal with the issue of the best and safest way to write a full text indexed search function. Its coming up though. But if I had the choice of writing 1 D-SQL routine and locking it down or writing 1,000 stored procedures that were table-specific, I think I'll go with D-SQL. Of course, I have the luxury of security testers who can be assigned to try to crack my code, and I realize that many of the folks here are one-man shows.

Now, it occurs to me that if hackers didn't get feedback through the .Net framework on the client when they forced SQL errors, SQL injection would be almost impossible. Is there some way to use compiler directives or attributes or something to prevent unhandled exceptions from bubbling up? Maybe replace it with a nice uninformative dialog box telling folks not to format their search strings in naughty ways and if they do it again they get their IP blocked?

|||

I learned to love dynamic SQL after I wrote a bunch of stored procedures using D-SQL and then ran into a problem that I had to try to track down and troubleshoot.. It's worth the extra time to spend to learn to do things the right way.

|||

I have seen a lot of novices fal into the trap. Things that are easiest to learn can get you into trouble that quickly too. D-SQL can be effective if used wisely. Same goes with CURSORs, TRIGGERs. It is very easy to write up a CURSOR than to figure out how to write a loop without CURSOR so novices fall into the trap and start using CURSOR for the simplest of the loops. I have seen people asking how to execute DTS package via TRIGGER. That was a new high for me.. There is always more than one way to skin a cat. Its worth the extra time to read up the best practices (even a quick google search brings up plenty of articles) and implement them and pays in the long run.

|||

Do you think we should start a D-SQL forum here? It seems like there's a lot of people wanting to know more about it and some people who are downright alarmed by it. Like fire, it is a wonderul servant and a terrible master...

|||

Erland has an excellent article:The Curse and Blessings of Dynamic SQL. Its long but worth reading.

|||

Yeah, you sent that to me 2 weeks ago. It was very good. Thanks again.

sql

Friday, March 9, 2012

excessive dtexec logging post SP2

Since the other related topic is closed/answered...

The Short version:

SQL is now logging too much info with every package. The volume of the new "User: Diagnostic" event has caused some packages to fail and the command-line exclusion option appears to have no effect on the events logged to the SQL provider. Is this a bug in dtexec or am I using the wrong syntax to exclude log entries? I don't want to modify all of my SSIS packages...

More Info:

SQL SP2 introduced new logging events, most of which appear to get logged by default. So far, none of our packages have used any sort of explicit logging configuration; it's all been set at the command line using a syntax like shown below:

dtexec.exe /FILE "D:\SSIS Packages\MyApp\Vendors.dtsx" /MAXCONCURRENT " -1 " /CHECKPOINTING OFF /REP E;Diagnostic /LOGGER "{6AA833A1-E4B2-4431-831B-DE695049DC61}";"MyDBConnName"

This does appear to correctly limit what gets logged to the console (and thereby the SQL Agent's job step log), but has no effect on what's logged to the database. Normally, I'd use /REP EWDCI, but I was attempting to limit the log entries to Errors only.

I first came across this error when a package failed, but it only logged the following to the console with nothing in sysdtslog90 (while not the "latest/greatest" server, this is a relatively low-utilized quad 2.8ghz xeon ProLiant DL580 G2):

Error: 2007-06-21 06:01:30.45
Code: 0xC0202009
Source: MYPACKAGENAME Log provider "{0C3CBE9B-D828-41C2-98D2-99BA498B314A}"
Description: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80004005 Description: "Connection is busy with results for another command".
End Error
Error: 2007-06-21 06:01:30.46
Code: 0xC0014010
Source: MYPACKAGESTEP Load
Description: The SSIS logging provider "{0C3CBE9B-D828-41C2-98D2-99BA498B314A}" failed with error code 0xC0202009 ((null)). This indicates a logging error attributable to the specified log provider.
End Error

I changed this one package to only log OnError events, but I'd rather not have to change every package to do the same, plus I'd like the ability to easily turn on verbose or any other logging level when needed.

At this point, fine-grained control of logging which is initiated external to the package itself, is basically only available programmatically. External logging can be accomplish by by implementing a logger, an event listener, by hooking/redirecting the byte stream produced by dtexec.exe, or by programmatically configuring the LoggingOptions class on the package object. Within the package itself is a differrent story, but it would seem you're looking for externalization of logging control.

dtexec's "/reporting" command line switch relates to console events and the "/consolelog" command line switch relates to fields which are sent to the console for a given event. To observe the applicability of the two switches, run dtexecui.exe, a gui based dtexec command line builder. Notice under the reporting node, the selectable events are listed as "console events".

EXCEPTION_ACCESS_VIOLATION Error 17883

We installed a Server 2003 Standard and SQL 2000 Sp3a
This is a replace of an NT4 Server With SQL 7.
The application has never had SQL related problems,
Now after 4 hours live production the SQL Server hung with dumps in the SQL
log's starting one hour before complete hung.
Errormessages :
Error 17883
EXCEPTION_ACCESS_VIOLATION
The sheduler appeers to be hung
Then a dump in the log
Same select statement with the same spid
over and again for about one hour then complete stop of SQL 2000
Customer not very pleased an lots of stress for the technical guys and the s
oftware guys.
Also Errors in the Application Log for SQL Server.
The machine is an Xeon Processor 2.8 Ghz (Server 2003 sees it as a dual proc
essor machine)
2 GB of Memory
360 MB dedicated for SQL 2000
100 GB raid 5 hard disk U320
Database size 450 MB
Happens on a simple select statement.
Thanks a lot
Luc VanleeuwI have had the same thing happen to me and after some research found the
following article:
http://support.microsoft.com/default.aspx?kbid=810885
I too just moved to Windows 2003 server with SQL and applied SP3a. I think
when I applied this patch it "blew away" my previous patch installation of
MS03-031: Cumulative Security Patch for SQL. I am going to apply this
tonight and see if it fixes my problem...let you know...or let me know if
you have any luck...
Jim
"Luc Vanleeuw" <anonymous@.discussions.microsoft.com> wrote in message
news:5AFD1596-C167-407C-9DBC-F01712DE600F@.microsoft.com...
> We installed a Server 2003 Standard and SQL 2000 Sp3a
> This is a replace of an NT4 Server With SQL 7.
> The application has never had SQL related problems,
> Now after 4 hours live production the SQL Server hung with dumps in the
SQL log's starting one hour before complete hung.
> Errormessages :
> Error 17883
> EXCEPTION_ACCESS_VIOLATION
> The sheduler appeers to be hung
> Then a dump in the log
> Same select statement with the same spid
> over and again for about one hour then complete stop of SQL 2000
> Customer not very pleased an lots of stress for the technical guys and the
software guys.
> Also Errors in the Application Log for SQL Server.
> The machine is an Xeon Processor 2.8 Ghz (Server 2003 sees it as a dual
processor machine)
> 2 GB of Memory
> 360 MB dedicated for SQL 2000
> 100 GB raid 5 hard disk U320
> Database size 450 MB
> Happens on a simple select statement.
> Thanks a lot
>
> Luc Vanleeuw
>