Skip to main content
September 9, 2022
Solved

Create Table As Select

  • September 9, 2022
  • 4 replies
  • 0 views

Hi, I need to build some tables in the application database. I am using a extensibility rule with a StringBuilder and the BRAPi.Database.ExecuteActionQuery command to do that.

The "Create table" command is working fine. Eg:
sqlScript.AppendLine("CREATE TABLE [PayrollMapping](")
sqlScript.AppendLine("[Payroll]  varchar(255),")
sqlScript.AppendLine("[Pay_Basis]  varchar(255),")
sqlScript.AppendLine("[Salary_Annual_Factor]  Int")
sqlScript.AppendLine(") On [PRIMARY]")

But I cannot make the "Create table as select" command work. Eg this does not work (Error: Incorrect syntax near the keyword 'Select')
sqlScript.AppendLine("CREATE TABLE [EBSRegister] As")
sqlScript.AppendLine("Select [RegisterID], [FirstName], [LastName]")
sqlScript.AppendLine("From XFW_PLP_Register")

Is the "Create table as select" not a valid command for the OneStream BRAPi.Database or is there simply something wrong with the syntax above?

Thank you

Best answer by AndreaF

After testing many suggestions I found that the following line of code does what I was looking for, i.e. it creates a table which is a copy of the old table, without copying any data from the old table (because of the Where 1 = 2 condition):

sqlScript.AppendLine("Select * Into EBSRegister From XFW_PLP_Register Where 1 = 2")

4 replies

September 9, 2022

Hello,

The "create table as select" looks correct but seems SQL system inside OneStream is not able to comprehend this. 

Not all SQL queries, vb.net functions would work as is inside OneStream.

September 9, 2022

You can try and create the table first and then do the insert like...

Using dbConn As DbConnInfoApp = BRApi.Database.CreateApplicationDbConnInfo(si)
'Create the table, only need this once
Dim sqlDM As String = "Create table EBSRegister ([RegisterID] varchar(255),[FirstName] varchar(255),[LastName] varchar(255))"
BRApi.Database.ExecuteActionQuery(dbConn, sqlDM,False, True)
'Insert the records into the new table
Dim sql As String = "insert into EBSRegister (RegisterID,FirstName,LastName) select RegisterID,FirstName,LastName from XFW_PLP_Register"
BRApi.Database.ExecuteActionQuery(dbConn, sql,False, True)
End Using

AndreaFAuthor
September 9, 2022

Hi and thank you for your replies. Yes your solution would work, but the reason why I was trying to avoid using the "Create table <table name> (<column1>, <column2>, etc.)" command is that the table I want to recreate has around 50 columns, so I was looking for a way to copy the structure from an existing table

AndreaFAuthorAnswer
September 9, 2022

After testing many suggestions I found that the following line of code does what I was looking for, i.e. it creates a table which is a copy of the old table, without copying any data from the old table (because of the Where 1 = 2 condition):

sqlScript.AppendLine("Select * Into EBSRegister From XFW_PLP_Register Where 1 = 2")

September 9, 2022

Yes you're on the right track though the new table won't have the indexes/constraints copied over.

The "Create Table As Select (CTAS)" only works in Azure Synapse Analytics Platform System aka PDW. Also works in oracle but not in SQL Server.

May 10, 2023

Insert into should work fine, did you try that?

AndreaFAuthor
May 10, 2023

Hi, yes the "insert into" command works in OneStream.

The command I've used to create the table is:  "Select * Into NewTableName From OldTableName Where 1 = 2"

June 10, 2025

I'm a bit late to the party, but you can do something like

Dim MyCopyTable as DataTable=BRApi.Database.GetCustomDataTable(si, "Application", "MySourceTable", lstWheres, lstOrderBys).Clone

Steve