Select row in SQL using GUID - sql

When creating the table I used
[Key] UNIQUEIDENTIFIER not null DEFAULT NEWSEQUENTIALID(),
I'm trying to use the following to select, I gave up on doing it in C# for now and am entering the query in MyLittleAdmin (the SQL database manager my hosting provider is using)
SELECT *
FROM GameList
WHERE Key = '9abc2cdc-1919-e611-80d3-008cfa5ae917'
I get the following error:
Msg 156, Level 15, State 1, Line number 1
Incorrect syntax near the keyword 'Key'.
I know I have the right GUID, using SELECT * FROM GameList returns the data and I have copied and pasted from it.

Key is a keyword and must be surroundted by string identifiers if you want to use it as a column name (like it is in the column definition):
SELECT *
FROM GameList
WHERE [Key] = '9abc2cdc-1919-e611-80d3-008cfa5ae917'

Related

Change SSRS parameter value into column name in query

I am trying to, without stored procedures, create a SQL query in an SSRS report and take a parameter value and use it as a column name. My question is how to I change the parameter value into the actual column name? I have tried the following query with the table identifier (i.e. t.#FilterColumn1) and without the table identifier (i.e. #FilterColumn1).
DECLARE #FilterColumn1 AS nvarchar(250)
SET #FilterColumn1 = 'Column1'
SELECT DISTINCT
t.#FilterColumn1
FROM [JetNavDwh2017].[dbo].[TableName] t WITH(NOLOCK)
When I do not use the table identifier the result is the the column name I defined. When I use the table identifier I receive the error message below, but I have not found a conclusive way to resolve the issue based on the error. Thank you in advance for your help!
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '#FilterColumn1'.
Msg 319, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

Getting Invalid Column Name in Azure SQL Data Warehouse

I created an alias and checked whether alias column is null or not null but alias is not working in sql data warehouse.
select (emp_id) a
from dbo.test b
where a is not null
Msg 207, Level 16, State 1, Line 1
Invalid column name 'a'.
You can't Use alias as simple as in where clause. But there are other ways which you can implement it
Using CTE
CROSS APPLY/OUTER APPLY
There are good examples at here for you to look-out

Invalid Identifier/Ambiguously Defined

I have a SQL statement which ends in:
where <table_name>.<column_name> = '<column_value>'
I get the following error:
ORA-00904: "table_name"."column_name": invalid identifier
However, I know that the column is valid for sure. I also tried:
where <schema><table_name>.<column_name> = '<column_value>'
but got the same error. Lastly I tried without the identifiers:
where <column_name> = '<column_value>'
but that results in an column is ambiguously defined error.
What am I missing here?
Whole Query:
SELECT r.<COLUMN_NAME_1>, r.<COLUMN_NAME_2>, etc, t_append.*
FROM (
SELECT <COLUMN_NAME_1>, r.<COLUMN_NAME_2>, etc..
FROM <TABLE_NAME> ) r
inner join <TABLE_NAME> t_append on
t_append.<COLUMN_NAME_1> = r.<COLUMN_NAME_1>
AND t_append.<COLUMN_NAME_2> = r.<COLUMN_NAME_2>
AND etc...
WHERE <TABLE_NAME>.<COLUMN_NAME_1> = '<COLUMN_VALUE1>'
AND <TABLE_NAME>.<COLUMN_NAME_2> = '<COLUMN_VALUE2>'
This query takes composite key columns and value and then returns the composite key values followed by the row data which the key represents.
Apart from the above mentioned suggesstions,there may two possibilities according to me. You may get
ORA-00904: "table_name"."column_name": invalid identifier
1) if you don't have necessary permissions on the accessing objects. (confirm your permission on the object)
2) if your column was defined with double quotes like below
create table test("CheckMyColumn" number));
then it will be case sensitive. (Refer the table definition and try with same case)
The reason of a column ambiguously is because oracle doesn't know which column you are referring , it seems in your query you have specify 2 tables( from tab1 , tab2 ).
As for "table_name"."column_name": invalid identifier it means for sure column_name column for table table name doesn't exists, can you provide the ddl of the table.

I want to create a new table from a SQL select statement?

I want to be able to create new tables based on any SQL select statement. I have tried the following which I got the format from another question and it does not work (there are similar questions but not one that I found actually works). I keep getting an error on the SQL statement.
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
This is the CREATE TABLE statement:
CREATE TABLE MyNewTable
AS
SELECT *
FROM dbo.Bat
This will copy the entire table including rows
SELECT *
INTO newTableName
FROM dbo.Bat
Add WHERE 1 = 0 to copy just the table structure
If it is SQL Server (the dbo schema, default in SQL Server indicates it is SQL Server), you can do following.
select * into MyNewTable from dbo.Bat;
The SELECT INTO statement does not copy your table constraints.
You statement is a valid Oracle and MySQL statement though.
CREATE TABLE ... AS SELECT is simple (by deliberately ignoring for example the concepts of storage)
To create a table with all its lines
code:
CREATE TABLE XX AS SELECT * FROM YY ;
the result of command in mysql

Select syntax as a subquery to insert into

Here's what I'm trying to do in SQL Query Analyzer.
Insert into db_name1.dbo.tb_name1 (select * from db_name2.dbo.tb_name2)
but I keep getting this error message
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ')'.
I wonder what went wrong. Does subquery isn't work in SQL server 2000?
What is the right syntax for this one? I would like to transfer all the values from a tables column of a database to another table of another database without creating any duplicate of its element but merely overwrites the current values.
This is your query as you think it should be interpeted:
Insert into db_name1.dbo.tb_name1
(select * from db_name2.dbo.tb_name2)
This is the query as the compiler sees it:
Insert into db_name1.dbo.tb_name1(select * from db_name2.dbo.tb_name2)
That is, the open parenthese after the table name means "the list of columns is starting here". But you don't have a list of columns. And select is not a valid column name. Hence the error.
This is easily fixed by removing the parentheses:
Insert into db_name1.dbo.tb_name1
select * from db_name2.dbo.tb_name2
However, it is more correct to include the column names being inserted:
Insert into db_name1.dbo.tb_name1(col1 . . .)
select * from db_name2.dbo.tb_name2;
It is a good idea to always include column names in insert statements. If you had done this, you wouldn't have had to ask this question.