79678617

Date: 2025-06-25 07:31:06
Score: 0.5
Natty:
Report link

The code with SQL Server Express LocalDB worked, this is the resulting entry in the .yml file

# Start local SQL Server instance
- task: CmdLine@2
  displayName: 'Start mssqllocaldb $(dbInstanceName)'
  inputs:
    targetType: 'inline'
    script: |
        sqllocaldb create "$(dbInstanceName)" 15.0.2130
        sqllocaldb share "$(dbInstanceName)" "$(dbInstanceShared)"
        sqllocaldb start "$(dbInstanceName)"
        
        setlocal enabledelayedexpansion
        for /f "delims=" %%a in ('sqllocaldb info $(dbInstanceName)') do set "ret=%%a"
        set "temp=!ret:*: =!"
        for /f "delims=" %%i in ("!temp!") do endlocal & set "instance=%%i"
        
        sqlcmd -S %instance% -q "CREATE LOGIN $(dbUser) WITH PASSWORD = '$(dbPassword)'"
        sqlcmd -S %instance% -q "CREATE USER $(dbUser)"

sqllocaldb info $(dbInstanceName) returns the instance name needed to access it with sqlcmd. The middle block of code just takes away the context information to leave me with only the result np:\\.\pipe\LOCALDB#<pipe name>\tsql\query

Now i can log in from any task with
sqlcmd -S (localdb)\.\$(dbInstanceShared) -U $(dbUser) -P $(dbPassword)
and execute any query with
sqlcmd -S (localdb)\.\$(dbInstanceShared) -Q "<query>"

And via

RESTORE DATABASE [myDB] FROM DISK = 'myDB.bak' 
WITH RECOVERY, REPLACE, 
MOVE 'myDB_Data' TO 'myDB.mdf', 
MOVE 'myDB_log' TO 'myDB_log.mdf'

i am able to restore the db and work on it

Thanks go to AlwaysLearning, since his comment enabled me to find out about the preinstalled SQL Server Express LocalDB and this related question

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: MaxH