Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 <Select 'anything' from table" >returns values.

Author  Topic 

Reprovo
Starting Member

8 Posts

Posted - 2014-01-30 : 14:59:34
Hi.
I'm trying to understand why I can enter a query such as:
 

select 5,"random"
from customers;


and get two columns with 5 and "random" in every respective column field.Why don't I receive a syntax error ?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-01-30 : 15:19:42
quote:
Originally posted by Reprovo
Why don't I receive a syntax error ?

Not to be captain obvious, but I can only assume the syntax is valid. Meaning you have a column named Random in your table Customers.

If you want the value "random" as a string, try using single quotes (') instead of double quotes ("). Double quotes are one way to specify a quoted identifier, which doesn't seem like what you want here.
Go to Top of Page

Reprovo
Starting Member

8 Posts

Posted - 2014-01-31 : 01:05:41
I don't have a table with "random".I can do the same with absolutely any string or number
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-31 : 07:45:08
quote:
Originally posted by Reprovo

I don't have a table with "random".I can do the same with absolutely any string or number


yes thats by design
reason is simple
select supports returning constant value
ie
SELECT 1,'Text value',102.5 etc

this can be utilized to populate table with mutiple values like

INSERT Table(COl1,Col2,Col3)
SELECT 1,'Text value',102.5 UNION ALL
SELECT 2,'Text value123',56.5
...


similarly you can intermix columns with constant values too
like


INSERT Table(COl1,Col2,Col3)
SELECT 1,TextColumn,102.5
FROM Table2...


so that it inserts constants 1 and 102.5 for 1st and 3rd columns
whereas it put actual column values from Table2 for column 2.



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Reprovo
Starting Member

8 Posts

Posted - 2014-01-31 : 09:20:14
Ok that clears things up for me! Thanks
Go to Top of Page
   

- Advertisement -