Syed Farjad Zia Zaidi Syed Farjad Zia Zaidi - 6 months ago 9
SQL Question

unexpected results with square brackets in SQL query

I have a database with a Table named

PaymentReciept
I was playing around and found some unexpected results from a simple select all query.

// Return 3139 rows
select * from PaymentReciept

// Return 3139 rows
select * from dbo.PaymentReciept

// Return 0 rows
select * from [dbo.PaymentReciept]


I don't understand why the query with square brackets return no results. I know that square brackets are used as delimiters for identifiers that use some special character or a keyword.

Can someone kindly explain to me what's going on or direct me to some article or a book where I can learn more about this behavior.

Answer

[dbo.PaymentReciept]

Means that SQL is querying a table whose name is dbo.PaymentReceipt

whereas

dbo.PaymentReciept

AND

PaymentReciept

Mean that SQL is querying a table called PaymentReceipt in the schema dbo

To use square brackets correctly, use

[dbo].[PaymentReciept]