Inside the SQL Server Management Studio I am trying a query like this:
In human words: Give me all rows from the table where the string in the column "name" is "alice".
My not working attempt on this SQL query:
SELECT * FROM MyTable WHERE ["name"]=alice
I am somehow lost on finding the right syntax for this. There is always errors when the column is a varchar type. The error is wiered: "...alice is not a valid column name.."
When the column is a real type it works. for example:
SELECT * FROM MyTable WHERE ["age"]=30
Hope someone can tell me what i am doing wrong here.
Thanks
SELECT * FROM [DATABASENAME].[dbo].[MyTable] WHERE [Name] = 'alice'
the brackets are used to disambiguate identifier names like feildnames, database names, schema names, etc from literal values. Whenever you use an (N)Varchar feild to compare it to a literal string, put the string in single quotes. Never put double quotes around an identifier. Be sure to fully qualify the table name, or use ause databaseName; GO
statement at the top of the document, or select the active database from the drop down at the top of the query pane, so sql knows what database you mean.