Azure SQL Data Warehouse Computed Column Error - sql

I am really not sure what the error below is, but this statement works perfectly in Azure SQL Database but not on Azure SQL Data Warehouse. Is there anything specific to computed columns in SQL Data warehouse?
Here is the simple create table statement I am trying
CREATE TABLE Authors
(
AuthorId int IDENTITY(1,1) NOT NULL,
FirstName nvarchar(100),
LastName nvarchar(100),
FullName AS (FirstName + SPACE(1) + LastName) -- computed column
)
And the error:
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 6, column: 11: Incorrect syntax near 'AS'.

see Migrating Computed Columns
Computed columns are not supported in memory-optimized tables.
However, you can simulate a computed column.
The reference suggests using a view to mimic the effect of non-persisted computed columns.
For persisted computed columns the reference proposes the use of stored procedures (one to insert another to update).
You would need to asses if the benefits of persisted data outweighs the simplicity of using views.

Related

Get generated key from column organized table in DB2 Warehouse

I am not able to get generated key from column organized table from an INSERT SQL statement run against IBM DB2 warehouse. I am using Java and JDBC driver. Everything works fine - I am able to connect to DB, create tables, insert data, I am just not able to get a generated key if it is generated in column organized table. Note that row organized tables work fine and return the key properly.
Consider a table:
CREATE TABLE users (
id INTEGER not null GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
username VARCHAR(16),
PRIMARY KEY (id)
);
If this is row organized table I am able to get the generated key fine by using:
PreparedStatement pr = connection.prepareStatement("INSERT INTO users(username) VALUES(?)", PreparedStatement.RETURN_GENERATED_KEYS);
However, If this is column organized table the PreparedStatemnt creation fails with an error:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-1667, SQLSTATE=42858, SQLERRMC=BLUADMIN.USERS;ORGANIZE BY COLUMN;FINAL|NEW|OLD TABLE, DRIVER=4.25.13
Even if I specify columns I want to get returned like so:
PreparedStatement pr = connection.prepareStatement("INSERT INTO users(username) VALUES(?)", new String[]{"id","username"});
pr.setString(1, "test");
pr.executeUpdate();
I get
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-1667, SQLSTATE=42858, SQLERRMC=BLUADMIN.USERS;ORGANIZE BY COLUMN;FINAL|NEW|OLD TABLE, DRIVER=4.25.13
on line pr.executeUpdate();.
Does this mean that it is not possible to get generated key from column organized table from the INSERT statement in DB2 Warehouse?
Currently shipping versions v11.1.x and V11.5.x will throw SQL1667N when the query sent to Db2 uses 'FINAL TABLE' or 'OLD TABLE', or 'NEW TABLE' clauses for a column organized table.
When you use the jdbc syntax PreparedStatement.RETURN_GENERATED_KEYS, this syntax may be used under the covers.
Currently those clauses are not supported (i.e. will cause the exception to be thrown) for ORGANIZE BY COLUMN tables. There are other restrictions on column organized tables that you should be aware of before using them.
You can workaround this by creating your tables explicitly with the ORGANIZE BY ROW clause.
Have you tried actually selecting the generated ID? Try something like this:
SELECT ID FROM FINAL TABLE
(INSERT INTO users(username) VALUES(?))
See "Retrieval of result sets from an SQL data change statement" in the IBM Db2 documentation.

Create a table in dbshell with django

here is my code :
frontgreat=> CREATE TABLE contact_titlemessagesuggestion;
ERROR: syntax error at or near ";"
LINE 1: CREATE TABLE contact_titlemessagesuggestion;
i don't understand why it's not working and why it's an syntax error.
frontgreat=> DROP TABLE contact_titlemessagesuggestion;
ERROR: table "contact_titlemessagesuggestion" does not exist
have no syntax error and work fine.
Regards
You can normally not create a table without any columns. Therefore one often either has to list the columns, or use for example a query that provides both data (but meta-data as well) to construct the columns in the table.
For example:
CREATE TABLE contact_titlemessagesuggestion (
pk INT
);

Getting the identity value from a table with single identity column in FitNesse

Does FitNesse/dbFit support inserting into a table with a single identity column and then returning that value into a variable?
For example:
create table TestTable ( ID int identity(1,1) )
!|Insert|TestTable|
|ID? |
|>>TestID |
results in the following exception:
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ')'.
Is there another way to accomplish this?
Your problem is not with Fitnesse, but with the SQL engine. If you try the same query in SQL Server Management Studio you will receive the same error. Thus, given the restriction that you cannot use set identity_insert on the question you really need to ask is how to insert a record with no insertable fields in SQL Server independent of Fitnesse? This StackOverflow post provides a simple answer:
INSERT INTO #TempTable DEFAULT VALUES
Having that in hand, now the task is to map this to Fitnesse. Here is one solution:
!|execute|CREATE TABLE #TestTable ( ID int identity(1,1) )|
!|execute|INSERT INTO #TestTable DEFAULT VALUES|
!|query|SELECT TOP 1 ##IDENTITY [ID] FROM #TestTable|
|ID? |
|>>TestID |
Fitnesse's INSERT command does not support the default construct, so you must switch to the more general EXECUTE command to do the insert. That, however, does not return results so you cannot glean the auto-inserted ID value directly. The final query gives you one way to grab the just-inserted identity value.

Set Identity in existing column of table in SQL Azure

I am facing one critical issue in SQL Azure database, i have created this database from local sql server database and in that one table i have set isidentity property.
Today i have deleted that table and created it again through SQL Azure portal but i could not able to set identity column on that! I have tried with sql query also like below
alter table mytablename alter column id identity(1,1)
gives me error like
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword
'identity'.
Please suggest me some technique by which i can alter this column and set isidentity property.
Thanks in Advance.
In this case i have use RazorSQL to alter this table and first dropped that table and recreate using sql query and it's worked

sql - retain calculated result in calculated field

certain fields in our database contain calculated functions e.g.
select lastname + ', ' + firstname as fullname from contact where contact.id =$contact$
when viewing the field the correct data is shown (i assume this is because when you open the record, the calculation is executed). however, the data is not 'stored' to the field, and therefore is null until the record is opened. is it possible to 'store' the result to the field, making it possible to search the data?
many thanks
james
EDIT
it is not possible for me to create computed_columns using our software.
the above field is a text feild where either 1) a user can manual type in the required data or 2) the database can generate the answer for you (but only whilst you are looking at the record). i know that if I run the following:
Select * from contact where contact.id =$contact$ for xml auto
i only get lastname, firstname - so i know that the fullname field does not retain its information.
If you are using computed columns in sql server, the column is already searchable regardless of whether the calculation result is stored or not. However, if you would like to make it so that the calculation is not run each time you read the row, you can change that under row properties in your Modify Table GUI.
Use the PERSISTED key word when you create the column
From BOL:
PERSISTED
Specifies that the SQL Server Database Engine will physically store the computed values in the table, and update the values when any other columns on which the computed column depends are updated. Marking a computed column as PERSISTED lets you create an index on a computed column that is deterministic, but not precise. For more information, see Creating Indexes on Computed Columns. Any computed columns that are used as partitioning columns of a partitioned table must be explicitly marked PERSISTED. computed_column_expression must be deterministic when PERSISTED is specified.
This isn't the way computed columns work in SQL Server, so I suspect this is something your client application is doing. How are you looking at the data when the value is computed correctly? Does it work when you view the data in SSMS?
Take a look at http://msdn.microsoft.com/en-us/library/ms191250(v=SQL.90).aspx to see how to create computed columns properly.
eg.
create table TestTable
(a int,
b int,
c as a + b)
insert into TestTable (a,b)
values (1,2)
select * from TestTable
where c = 3
This query is based on the computed column and it returns the row that's been inserted.
You need to use the PERSISTED option on a column when you use CREATE TABLE e.g.
CREATE TABLE test (col_a INT, col_b INT, col_c AS col_A * col_B PERSISTED)