MSSQL - Declaring variables across multiple sql files

Hello!

Is there a way in MSSQL liquibase to declare a variable from one sql and then used in another sql?

Example below where first sql declared @User, but second sql will error with “Must declare scalar variable @User

I need to define them separately because the @User is used multiple times in my project.

I also cannot use the liquibase properties substitution because we need the original sql files to be still compatible with non-liquibase execution.

<databasechangelog>

	<changeSet  author="liquibase-docs"  id="sql-example">
	  <sql>
		  SET QUOTED_IDENTIFIER ON

		  declare @User sysname
		  select @User = user_name()
	  </sql>

	  <sql>
	  	select @User
	  </sql>
	</changeSet>

</databasechangelog>

Thank you.

Hi @francis Thanks for joining the community. If you can’t use Liquibase variable substitution and you must leave the files as-is for non-liquibase execution, then you can optionally purchase Liquibase Pro which has a runWith sqlcmd which leaves the files as-is and runs using sqlcmd.exe .