![]() ![]() PRINT statement: CREATE TABLE ANLA(GEGST VARCHAR(8), GPLAB DATE, GRBLT VARCHAR(5), GRBND VARCHAR(5), KTOGR VARCHAR(8), LAND1 VARCHAR(3), MENGE NUMERIC(16,3)) ĬREATE TABLE AUT_C_TABLOG(ORIGINAL VARCHAR(1), PROTOCOL VARCHAR(1), TABNAME VARCHAR(30)) WHEN RN = 1 THEN N') CREATE TABLE ' + Table_N + N'(' + STUFF(ColumnDefinition, 1, 2, N'') WHEN DLength IS NULL THEN N', ' + Column_N + N' ' + DTypeĮLSE N', ' + Column_N + N' ' + DType + N'(' + DLength + N')' ROW_NUMBER() OVER (PARTITION BY Table_N ORDER BY Table_N) RN, What is needed here is to number rows for each different table name, and then, when the row number is 1, to include CREATE TABLE part of SQL statement. You may try to generate a dynamic statement and execute this statement. Table Name: DDC_Loop(Sample below with 2 tables metadata) Table_N Column_N DType DLength Also, not a big fan of cursor, so if you have an entirely different approach of this then please feel free to advise. If I concatenate a column that has VARCHAR for DType and 100 for DLength then + '(' + ')' would result in VARCHAR(100) but with DATE,INT or any similar Datatypes, I need a different approach. For example, if the data type for a column is say DATE then the Dlength value for that column would be NULL. ![]() So far I have added a cursor to loop through them and hit a roadblock with Dynamic SQL syntax expecially dealing with NULL values for certain column's values for DLength field. Basically a typical table structure or definition. I have a metadata table which has four columns Table_N, Column_N, DType & DLength, I am trying to loop through the metadata table and for each distinct table dynamiaclly spit CREATE TABLE statement with all of its columns, datatypes and datalength. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |