|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
validate statement before execute with sp_executesqlIs there a way to validate or prepare a sql statement built dynamically.
By validating it I mean making sure the table and field names are correct, the data types match, but not actually execute it. Ideas, suggestions ... much appreciated. Gigel newbie wrote:
> Is there a way to validate or prepare a sql statement built dynamically. Good Question. I would also like an answer. I do not know of a way to> By validating it I mean making sure the table and field names are correct, > the data types match, but not actually execute it. > > Ideas, suggestions ... much appreciated. > > Gigel do this. The best I know is to tighten and control the sql generation process to the degree necessary that requires no validation. You would need to validate your metadata exists prior to the generation of sql unless you know absolutely sure this is also a given. Hey Ken,
One way I thought of is by first executing the query in a transaction that gets rolled back and read the @@ERROR. When you get a 0 you could go ahead and execute it. But there is a drawback to this and I am looking for a "lighter" solution. Gigel Show quoteHide quote "Ken" <kshap***@sbcglobal.net> wrote in message news:1158087876.662064.34560@e63g2000cwd.googlegroups.com... > > newbie wrote: >> Is there a way to validate or prepare a sql statement built dynamically. >> By validating it I mean making sure the table and field names are >> correct, >> the data types match, but not actually execute it. >> >> Ideas, suggestions ... much appreciated. >> >> Gigel > > Good Question. I would also like an answer. I do not know of a way to > do this. The best I know is to tighten and control the sql generation > process to the degree necessary that requires no validation. You would > need to validate your metadata exists prior to the generation of sql > unless you know absolutely sure this is also a given. > SET PARSEONLY ON
<your sql ...> James Show quoteHide quote "newbie" wrote: > Is there a way to validate or prepare a sql statement built dynamically. > By validating it I mean making sure the table and field names are correct, > the data types match, but not actually execute it. > > Ideas, suggestions ... much appreciated. > > Gigel > > > James Ma wrote:
Show quoteHide quote > SET PARSEONLY ON Hey Guys,> <your sql ...> > > James > > "newbie" wrote: > > > Is there a way to validate or prepare a sql statement built dynamically. > > By validating it I mean making sure the table and field names are correct, > > the data types match, but not actually execute it. > > > > Ideas, suggestions ... much appreciated. > > > > Gigel > > > > > > Good feedback! newbie (new***@newbie.com) writes:
> Is there a way to validate or prepare a sql statement built dynamically. The best bet is probably to run the batch bracketed by SET FMTONLY ON and> By validating it I mean making sure the table and field names are correct, > the data types match, but not actually execute it. SET FMTONLY OFF. The purpose of SET FMTONLY is to find any result sets the batch may create. In this mode SQL Server sifts through the code, so it will find misspelled table names (which is the one thing which is hardest to verify). Unfortunately, FMTONLY can also cause some false alarms. Some errors like a mess-up in data types may not turn up until run-time as SQL Server unfortunately permit to many implicit conversions. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx Just found this also works: SET NOEXEC ON
Thanks for your sharing. James Show quoteHide quote "Erland Sommarskog" wrote: > newbie (new***@newbie.com) writes: > > Is there a way to validate or prepare a sql statement built dynamically. > > By validating it I mean making sure the table and field names are correct, > > the data types match, but not actually execute it. > > The best bet is probably to run the batch bracketed by SET FMTONLY ON and > SET FMTONLY OFF. > > The purpose of SET FMTONLY is to find any result sets the batch may > create. In this mode SQL Server sifts through the code, so it will find > misspelled table names (which is the one thing which is hardest to verify). > Unfortunately, FMTONLY can also cause some false alarms. > > Some errors like a mess-up in data types may not turn up until run-time > as SQL Server unfortunately permit to many implicit conversions. > > -- > Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se > > Books Online for SQL Server 2005 at > http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx > Books Online for SQL Server 2000 at > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx > James Ma (Jame***@discussions.microsoft.com) writes:
> Just found this also works: SET NOEXEC ON SET NOEXEC ON will not trap misspelled table names.-- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
A .NET Framework error occurred during execution of user defined r
sql statement (how to) Update Information in SQL from a VBS script Return a City in a column on max count from another column in a group by Need help with select statement Converting Orcale DECODE stmt to T-SQL Dynamic SQL load into dynamic table SQL Select using parameter Setting up the SQL Server alias programatically Query question |
|||||||||||||||||||||||