0

I want to execute a procedure from another procedure for that I want to enter database name dynamically.

            SELECT @dname=dbname FROM @db WHERE id=@intFlag
            PRINT @dname
            EXEC [@dname].[dbo].[IPC_GetCount] @UserLogin,@Role,@Email,@Days,@Code,@Status

But it is not accepting the variable even though I am getting db name inside the variable.

Please help

3
  • for that you would need dynamic SQL Commented Dec 19, 2018 at 10:36
  • can you please help with the code Commented Dec 19, 2018 at 10:37
  • See learn.microsoft.com/en-us/sql/relational-databases/… - you just build a string containing your SQL and pass it to that procedure. Commented Dec 19, 2018 at 10:40

1 Answer 1

1

Can you try this query

SELECT @dname=dbname FROM   @db WHERE  id=@intFlag

PRINT @dname

DECLARE @query varchar(100) = 'EXEC ['+ @dname +'].[dbo].[IPC_GetCount] '+ @UserLogin +',' + @Role+',' + @Email +',' + @Days+',' +@Code +',' +@Status

EXECUTE (@query) 
Sign up to request clarification or add additional context in comments.

4 Comments

giving error invalid column name "Invalid column name '@dname'."
can you try now by changing [@dbname] to @dbname
can you print @query variable and include in comment? Try with query execute in your database see results
Then some of your variables are NULL please verify your variables are not NULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.