Home > database >  The following code execution errors, need how to change
The following code execution errors, need how to change

Time:03-15

DECLARE @ I INT, @ oldName NVARCHAR (30), @ newName NVARCHAR (30), @ SQL varchar (8000)
SELECT @ I=1

Select @ SQL='
WHILE @ i<181 and (select col '+ cast (@ I as varchar (10)) +' FROM imp_sheet1 WHERE id=1 and isnull (col '+ cast (@ I as varchar (10)) +', ' ' ' ') & lt;> ' ' ' ')
The BEGIN
SELECT @ oldName=imp_sheet1. Col '+ cast (@ I as varchar (10)) +'
, @ newName=(select col '+ cast (@ I as varchar (10)) +' FROM imp_sheet1 WHERE id=1)
The EXEC sp_rename @ oldName, @ newName
@ I=@ I + 1
END
'
The exec (@ SQL)

CodePudding user response:

Table structure is as follows:

ID col1 col2 col3 col4 col5
Import duty number 1 factory contract name referred to as the customs declaration name
Dongguan 1511902001 RBDPalmStearine RPS 2 solid palm stearin

CodePudding user response:

Use the following
try?
 
DECLARE @ SQL varchar (8000), @ I INT
The SET @ I=1

WHILE @ i<181

The BEGIN

The SET @ SQL='
DECLARE @ oldName NVARCHAR (30), @ newName NVARCHAR (30)

IF the EXISTS (SELECT 1 FROM imp_sheet1 WHERE id=1 and isnull (col '+ cast (@ I as varchar (10)) +', ' ' ' ') & lt;> ' ' ' ')

The BEGIN
SELECT @ oldName=imp_sheet1. Col '+ cast (@ I as varchar (10)) +'
, @ newName=(select col '+ cast (@ I as varchar (10)) +' FROM imp_sheet1 WHERE id=1)
The EXEC sp_rename @ oldName, @ newName
END
'

The SET @ I=@ I + 1

END

CodePudding user response:

If no, the exec SQL) (@ line need to add in

CodePudding user response:

Exec (SQL) @ @ in the SQL should be constant, the value is known,
And you begin the end where the variable @ I
  • Related