select*from tablename -- without spaces in-between the star symbol
select * from tablename -- Applied spaces in-between the star symbol
Both queries are working in my Sql-Server-2016 query window. What is the difference and which situation we need to use this? and why Sql server
accept this?
There is no difference. As with many programming languages, T-SQL is parsed from input composed of strings. During parsing, many different types of "things" may be pulled from the text, such as KEYWORDs (such as select
and from
) and ASTERISK (*
).
It's simply the fact that there is no need for whitespace here since an asterisk character cannot be part of any KEYWORD or unescaped IDENTIFIER (such as a column or table name)1, and so there's no parsing ambiguity that whitespace would be required to help resolve.
1It's not uncommon (cannot speak for T-SQL specifically) that the parser uses a single pattern just for IDENTIFIERs and a later step actually distinguishes IDENTIFIERs that are in a specifically enumerated list are in fact KEYWORDs.