0

I need to create a table at runtime in order to create a stock book. Column names are the Item codes. Initial table is created as follows,

'Create the new table
strSQL1 = "CREATE TABLE " & Trim(dbName) & ".dbo.TB_ST_BOOK (" &
              "id int IDENTITY(1,1) PRIMARY KEY, " &
              "Entry  VARCHAR(30), " &
              "entry_date DATETIME, " &
              ") "
obj.CommandText = strSQL1
obj.ExecuteNonQuery()

After that, I use a loop to create the rest of the columns using item codes which exists in another table.

'Add new columns as a loop
Dim tempTb As DataTable
Dim sqlString = "SELECT IT_CODE,IT_NAME FROM TB_ITEMS"
tempTb = myTbClass.myFunctionFetchTbData(sqlString)

For i = 0 To tempTb.Rows.Count - 1
    strItCode = Trim(tempTb.Rows(i).Item("it_code")).ToString
    strSQL = "ALTER TABLE TB_ST_BOOK ADD IT_" & strItCode & " VARCHAR(20)"
    obj.CommandText = strSQL
    obj.ExecuteNonQuery()
Next

My concern is what could happen if the number of columns exceed 1024 limit (when no of items are more than 1024)?. I read that creating a wide table will resolve the issue. If so, how can I modify the above code to create a wide table?. Will I have to change my Insert, update and delete codes once i change it to a wide table?

enter image description here

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • 5
    *"Column names are the Item codes. Initial table is created as follow"* That's your problem. That's a design flaw. Normalise your data. – Thom A Aug 25 '20 at 10:22
  • can't normalize. Its a stock book – lakshitha dilhan Aug 25 '20 at 10:23
  • As for what would happen, a table **cannot** have more than 1024 columns. This is [documented](https://learn.microsoft.com/en-us/sql/sql-server/maximum-capacity-specifications-for-sql-server?view=sql-server-ver15#-objects) under the heading "Columns per table". – Thom A Aug 25 '20 at 10:23
  • 3
    *"can't normalize. Its a stock book"* Yes, you can, and it is **exactly** what you need to do. – Thom A Aug 25 '20 at 10:24
  • Can you kindly elaborate more. I have attached the expected final result as an image. – lakshitha dilhan Aug 25 '20 at 10:29
  • 3
    The Columns names shouldn't be a Item Code, you should have a column which stores the name of the Item Code, and a new row for each Item. – Thom A Aug 25 '20 at 10:30
  • 3
    Sure you can normalize this. The database doesn't need to be in the same format as your presentation layer. – Wouter Aug 25 '20 at 10:30
  • As for getting the result set you want, then you can use a dynamic pivot, such as shown in this question: [Efficiently convert rows to columns in sql server](https://stackoverflow.com/q/15745042/2029983). There are 10's, if not 100's, of other examples on how to dynamically pivot your data in SQL Server on Stack Overflow as well. – Thom A Aug 25 '20 at 10:33
  • 2
    @lakshithadilhan There are quite a lot of short tutorials on database normalization, for example [Database Normalization in SQL with Examples](https://www.sqlservercentral.com/articles/database-normalization-in-sql-with-examples). – Andrew Morton Aug 25 '20 at 10:40

0 Answers0