Home > database >  Ask how to write a dynamic field of splicing statement
Ask how to write a dynamic field of splicing statement

Time:12-11

Have a table fields are similar but the serial number is different, for example: item1, unitprice1, usage1, finalprice1, item2, unitprice2, usage2 finalprice2... ... . Item15... Finalprice15.

@ ITEM - as the input parameter
Want to use the following statement cycle
DECLARE @ n int, @ SQLS NVARCHAR (4000)
The set @ n=1
While @ n
=15The BEGIN
The set @ SQLS="INSERT INTO Table1 (item, unitprice, usage, finalprice) '
The set @ SQLS=@ SQLS + 'select' + @ ITEM,,,,,,,,,,,,,,,,,,


The exec (@ SQLS)
Select @ n=@ the n + 1

END

How to write the SQL splicing statements. Another @ n is int, can't seem to and field splices,

Thank you very much,

CodePudding user response:

'item' + cast (@ n as varchar (5)) can be applied

CodePudding user response:

Set
@ SQLS="INSERT INTO Table1 (item + cast (@ n as varchar (5)) +
'and unitprice' + cast (@ n as varchar (5)) +
'and the usage of' + cast (@ n as varchar (5)) +
', finalprice '+ cast (@ n as varchar (5)) +') '

Words what this circular insert is to be inserted, @ what ITEM
  • Related