sp_rename in SQL server inside an exec

1

I want to use the sql server sp_rename function, the problem is that I have to put it inside an exec, since the database on which it runs is dynamic and I have it inside a cursor. The fact is that the following sentence works correctly:

EXEC (@name+'..sp_rename "valores", "valores2"')

Since @name is the cursor variable indicated by the database on which it applies.

But if I try to do this

EXEC(   'select ''hola''; '
        [email protected]+'..sp_rename "valores", "valores2"
        ')

I need to put a select before the execution of sp_rename but it does not work.

It has to be something of the syntax, but I'm going crazy and I do not get it.

If someone can give me a hand I would appreciate it.

    
asked by user2506141 25.04.2018 в 18:32
source

1 answer

2

Good day about your concern you can do the following:

DECLARE @consultaDinamica varchar(50);

SET @consultaDinamica = 'SELECT GETDATE() AS fechActual;';

EXEC (@consultaDinamica+' EXEC sp_rename "valores", "valores2"');

What happens is that since you are using an exec in the same way when you run you have to add the internal exec along with your instruction and in this way provide a response and make the query ... as an example add it to the getDate but in your case it would be to place what should go in your select according to the logic that you require.

I hope you find it useful, greetings.

    
answered by 25.04.2018 / 18:51
source