2017-03-16 37 views
1

我有一個問題,它非常類似於我的舊問題Using variable in sql postdeployment build script? 但現在我不想做一個if/else並在發佈的腳本中包含sql代碼。我不想分享客戶之間的信息。發佈時如何不包含sql文件內容?

所以......這裏去

  • 我在Visual Studio數據庫項目。
  • 我也有2個客戶提供不同需求

我知道我可以

  • 使用post.deployment腳本,包括我需要在Project.Properties設置varible SQL文件的內容。」 SQLCMD變量「。

enter image description here

,然後用它像這樣Script.PostDeployment.sql

:r ..\$(Customer)Setup.sql 

在那裏我有2個文件

  • Customer1Setup.sql
  • Customer2Setup.sql

然後我將SQLCMD變量設置爲$(Customer)「Customer1」或「Customer2」,並且只包含我在SQLCMD中放置的那個變量。

,當我想用​​變量的出版窗口默認一個覆蓋它

enter image description here

我知道我可以在構建腳本做到這一點somewho但我想要知道我是否可以使用當前工具並創建Customer1.publish.xml和Customer2.publish.xml文件,以便我知道如果發佈到Customer2,Customer1的某些內容不會與該腳本一起使用。

編輯1

我創建了一個公共GitHub repository來試圖解決我的問題。希望有人可以看看並編輯它的工作。

編輯2

這是我的測試項目的結果(與我的真實的),其中在3次測試我的每一個存在導致相同的。 Customer2/Customer3中的所有「材料」都包含在結果中。如果這是這樣運作的有一些開關或設置在I'm我的設置丟失(VS2015的數據庫項目)

SET NOEXEC ON 
Print 'This print statement should not be in the publishing script if  Customer variable is not customer1' 
print 'Customer1 stuff from Customer1.sql' 
SET NOEXEC OFF 

IF('$(Customer)' <> 'customer2') 
SET NOEXEC ON 
Print 'This print statement should not be in the publishing script if  Customer variable is not customer2' 
print 'Customer2 stuff from Customer2.sql' 
SET NOEXEC OFF 

IF('$(Customer)' <> 'customer3') 
SET NOEXEC ON 
Print 'This print statement should not be in the publishing script if Customer variable is not customer3' 
print 'Customer3 stuff from Customer3.sql' 
SET NOEXEC OFF 

編輯3

所以,如果我想象這個好一點 這是什麼我想獲得 enter image description here

但這我所得到的 enter image description here

+0

可能的複製http://stackoverflow.com/questions/7151021/conditional-logic-in-postdeployment-sql-script -using-sqlcmd) –

+0

作爲腳本的一部分的條件邏輯有什麼問題? –

+0

就像我在聊天中所說的:有時候,客戶端會在腳本中設置腳本,然後我不希望腳本中有任何其他客戶特定的東西。我只是想做出這個自動解決方案。但是我認爲這可能/應該只是在構建腳本中完成(Cake?)。但我發現奇怪的是,這並不是內置於工具中的。也許我應該寫一個VS數據庫擴展? – Sturla

回答

0

下面將無法工作按預期:

:r ..\$(Customer)Setup.sql 

相反,雖然,你可以使用IF ELSE方法:

IF ('$(Customer)'='customer1') 
BEGIN 
    :r .\script_customer1.sql 
END 
IF ('$(Customer)'='customer2') 
BEGIN 
    :r .\script_customer2.sql 
END 

多一點研究,我發現similar SO question後。事實證明,如果在腳本中使用GO,以上方法將不起作用。因此,可以採用以下方式:

IF ('$(Customer)'<>'customer1') 
    SET NOEXEC ON 

:r .\script_customer1.sql 
SET NOEXEC OFF 

IF ('$(Customer)'<>'customer2') 
    SET NOEXEC ON 

:r .\script_customer2.sql 
SET NOEXEC OFF 
[在PostDeployment.sql腳本中使用SQLCMD條件邏輯(的
+0

好的真棒!我已經搜索和搜索,但沒有找到NOEXEC部分!我會立即嘗試這個。 – Sturla

+0

好吧,這是行不通的。或者至少我沒有做對。我添加了一個鏈接到一個公共的GitHub倉庫,你可以測試它,並希望儘可能地更新。我指望你Alex :-) – Sturla

+0

答案中的兩個選項都已經過測試。你有沒有嘗試過「生成腳本」選項,然後檢查它,並可能運行在SQL管理工作室? –