how to create a new table with the same schema as an existing table in dolphindb?
I have established a table in dolphindb, and now I want to establish another empty table with the same schema. what should I do?
The field definition of the original table can be obtained through “schema().colDefs”:
pt = loadTable("dfs://GPS_data", "pt")
shema = pt.schema().colDefs
col1 = exec name from shema
col2 = exec typeString from shema
t = table(10:0, col1, col2)
Or obtain it through an invalid where condition:
t=select * from pt where Send_time=1970.01.01T00:00:00.000
This is the query:
SELECT ROUND(column_1,0) as new_column_to_save
FROM table_to_save_to
Thank you
if you wanna insert it into a new table :
INSERT INTO NEWTableName ( colName)
SELECT ROUND(column_1,0) as new_column_to_save
FROM table_to_save_to
If you want to update a column within the same table
UPDATE table_to_save_to
SET new_column_to_save = ROUND(column_1,0)
FROM table_to_save_to
Can you issue a FORALL bulk INSERT into a table with an unknown structure? That means, can you build dynamically the INSERT command in the FORALL construct without knowing the number of fields at compile time?
Number and name of fields is retrieved at runtime and stored in a collection:
TYPE RowType is TABLE OF VARCHAR2(50) INDEX BY VARCHAR2(50);
TYPE TableType is TABLE OF RowType;
my_table TableType;
So at runtime my_table could be filled this way for example:
my_table(1)('FIELD1') = 'VALUE1A';
my_table(1)('FIELD2') = 'VALUE2A';
my_table(1)('FIELD3') = 'VALUE3A';
my_table(2)('FIELD1') = 'VALUE1B';
my_table(2)('FIELD2') = 'VALUE2B';
my_table(2)('FIELD3') = 'VALUE3B';
my_table(3)('FIELD1') = 'VALUE1C';
my_table(3)('FIELD2') = 'VALUE2C';
my_table(3)('FIELD3') = 'VALUE3C';
The insert statements that should be bulk executed therefore are:
INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1A,VALUE2A,VALUE3A);
INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1B,VALUE2B,VALUE3B);
INSERT INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1C,VALUE2C,VALUE3C);
EDIT: Do you even read the questions or you just read a couple of words in the title? The linked question asks how to bind a variable, this question asks how to bulk issue dynamic statements. Yes, there are the words 'insert' and 'table' in both questions.
No, you can't dynamically build and execute a FORALL...INSERT... statement dynamically. You can, however, build up an INSERT statement dynamically of the form:
INSERT ALL
INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1A,VALUE2A,VALUE3A)
INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1B,VALUE2B,VALUE3B)
INTO TABLENAME (FIELD1,FIELD2,FIELD3) VALUES (VALUE1C,VALUE2C,VALUE3C)
Or if the data you want to insert into your table resides in another table you might find an INSERT...(subquery) statement like
INSERT INTO TABLENAME
SELECT FIELD1, FIELD2, FIELD3
FROM OTHER_TABLE
WHERE something <> something_else
or you might be able to use a MERGE statement similar to
MERGE INTO TABLENAME t
USING (SELECT FIELD1, FIELD2, FIELD3 FROM OTHER_TABLE) o
ON (t.FIELD1 = o.FIELD1)
WHEN NOT FOUND THEN
INSERT (FIELD1, FIELD2, FIELD3) VALUES (o.FIELD1, o.FIELD2, o.FIELD3)
which will do a mass insert based on the data specified in the USING clause and the match criteria in the ON predicate.
So there may be ways to do what you want but without knowing the specifics of the source of your data and how you're manipulating that data prior to inserting it into your database it's tough to say whether or not any of them would apply.
Best of luck.
How to get the value after updating the table using triggers in sql
Having a scenario like this:
CREATE TABLE #tmp (id INT, txt VARCHAR(10))
INSERT INTO #tmp ( id, txt )
VALUES ( 1, 'abc')
You can return the values using OUTPUT (https://msdn.microsoft.com/en-us/library/ms177564.aspx) during an UPDATE like this:
UPDATE #tmp SET
txt = 'xyz'
OUTPUT INSERTED.id, INSERTED.txt
You can return any column from your table - INSERTED will contain new data while DELETED will contain previous data.
In the Update SQL it self you can use OUTPUT.
In the triggers on MS SQL servers there are to temp tabels called INSERED and DELETED see her for more info: https://msdn.microsoft.com/en-us/library/ms191300.aspx
Hope it helps.
Is it possible in SQL Server 2008+ to force an UPDATE statement on table to be transformed into INSERT statement, thus creating new row with the old and updated columns?
Yes this is a typical scenario for an INSTEAD OF UPDATE TRIGGER.
Create the following trigger on your table and it will insert a row for each update made on your table. you can have a bit more logic inside your trigger but this is just a basic definition just to give you some idea.
Inside your INSTEAD of UPDATE trigger you will have access to two system tables Inserted and deleted.
Inserted table will hold new values for the row that was being updated by the Update statement.
Deleted table will hold Old values for the row that was being updated by the Update statement.
Demo Trigger
CREATE TRIGGER tr_Table_Instead_Of_Update
ON TABLE_NAME
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO TABLE_NAME(Col1, Col2 , Col3)
SELECT Col1, Col2 , Col3
FROM inserted
END