共用方式為


在資料 API 產生器中使用預存程序

預存程序可以在 DAB 中公開為 REST 或 GraphQL 端點。 這適用於涉及自訂邏輯、篩選、驗證或計算結果的情況,而這些計算結果無法透過簡單資料表或檢視來處理。

設定

若要公開預存程序:

  • source.type 設定為 "stored-procedure"
  • 設定 source.object 為完整流程名稱
  • 定義選用的parameters並在必要時使用其預設值
  • 設定 rest.methods (例如, "GET""POST") 或 rest: false
  • 設定 graphql.operation"query""mutation",或省略為預設為 "mutation"
  • 使用"execute"動作授予許可權

CLI 範例

dab add GetCowrittenBooksByAuthor \
  --source dbo.stp_get_all_cowritten_books_by_author \
  --source.type "stored-procedure" \
  --source.params "searchType:default-value" \
  --permissions "anonymous:execute" \
  --rest.methods "get" \
  --graphql.operation "query"

設定範例

"GetCowrittenBooksByAuthor": {
  "source": {
    "type": "stored-procedure",
    "object": "dbo.stp_get_all_cowritten_books_by_author",
    "parameters": {
      "searchType": "default-value"
    }
  },
  "rest": {
    "methods": [ "GET" ]
  },
  "graphql": {
    "operation": "query"
  },
  "permissions": [
    {
      "role": "anonymous",
      "actions": [ "execute" ]
    }
  ]
}

REST 支援

  • 僅支援 GETPOST
  • 預設為 POST,如果methods被省略
  • 傳送帶有GET的查詢字串參數
  • 透過 JSON 內文傳送參數POST
  • 如果設定了 "rest": false,將停用預存程序的 REST 接口。

範例要求

GET /api/GetCowrittenBooksByAuthor?author=asimov

POST /api/GetCowrittenBooksByAuthor

{
  "author": "asimov"
}

GraphQL 支援

  • 需要 graphql.operation"query""mutation"
  • 欄位會自動加上 execute,例如 executeGetCowrittenBooksByAuthor
  • 參數會以 GraphQL 引數的形式傳遞

範例 GraphQL

query {
  executeGetCowrittenBooksByAuthor(author: "asimov") {
    id
    title
  }
}

局限性

  • 只會傳回第一個結果集
  • 不支援分頁、篩選和排序
  • 不支援關係
  • 需要中繼資料來自 sys.dm_exec_describe_first_result_set
  • 無法依鍵傳回單一項目
  • 無參數級授權