What I was trying to do is to create a staging table only with the primary key from source table.
For example, I have an address table with
create table dbo.Address
(
AddressId int Primary Key,
City varchar(10),
State varchar(10)
);
And I want to create the table
CREATE TABLE Staging.AddressPK (AddressId INT PRIMARY KEY);
My Biml script is like this.
CREATE TABLE Staging.<#=tbl.Name#>PK ( <#=tbl.GetColumnList(c => c.IsUsedInPrimaryKey)#> INT PRIMARY KEY);
It is working fine only if the primary key type is INT. So I want to know is there a way to make it dynamic?
2条答案
按热度按时间e3bfsja21#
The challenge here is the method you're using,
GetColumnList
is going to return a string (with the column names in it).You need to work with two elements: column name and the type.
Set up
Here's some static Biml to define our tables.
T0
has a composite key ofCol1
&Col2
. TableT1
has a single column primary keyCol0.
Working with the columns collection
This is an incomplete solution. Currently, it emits the data type as the SSIS type but you'll need to tune it for the target database.
The output for our sample data is
There might be Extension methods in the Biml something extensions library that gets you there.
An alternative approach I couldn't make work was make a copy of the existing table and then just remove the columns that weren't in the PK. Or create an empty table and then load it with just the PK columns. Either way, you could then leverage the existing
GetDropAndCreateDdl
extension method to build the SQL.ewm0tg9j2#
You should be able to select the data type from INFORMATION_SCHEMA.COLUMNS.