13

Is there the option to check syntax after I have completed create a query? If so, where can I find it? What does it validate and what does it not validate?

2
  • What tool are you writing your queries in? Commented Feb 19, 2012 at 19:59
  • @Joachim Isaksson - Microsoft SQL Server Management Studio Commented Feb 19, 2012 at 20:19

2 Answers 2

24

You can click the Parse query button in Management Studio. It's the blue check mark on the toolbar (you can also use Ctrl + F5):

parse.png

This only validates syntax, and doesn't check that the objects you've referenced exist, that joins are valid, etc. For example the following parses correctly since deferred resolution assumes that by the time you run the query "for real" the object will exist:

SELECT foo FROM dbo.table_does_not_exist;

This also passes parsing:

SELECT d.foo 
FROM x.dbo.does_not_exist AS d
INNER JOIN sys.objects AS s
ON d.blat = s.bar;

Even though sys.objects exists but does not contain the column bar.

It is essentially the same mechanism that allows you to compile a stored procedure that references objects that don't exist yet (which of course will fail at runtime).

Sign up to request clarification or add additional context in comments.

1 Comment

It is time for Microsoft to offer a way to check invalid object names and semantics. Anyone reading this who might have an outlook/hotmail/live account please vote up this feature and we will have a nice parse feature: connect.microsoft.com/SQLServer/feedback/details/361775/…
5
SET FMTONLY  ON 
--Your Query Here

This will validate your objects as well. It will give you the error if you do not have that object present in your database. It will not execute your Query on the database but only parse it and validate syntax and objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.