Pages

2011年11月14日 星期一

SQL語法中使用字串執行SQL語法


通常在資料庫的表格中有可能存在一些SQL的部分語法來讓程式存取使用。
但在寫Trigger或 Store Procedure時要如何使用這些字串?

正常來說直接使用exec sp_executesql @sqlcmd會出現下面的系統提示:
必須宣告純量變數@studentno。

因此需要做一些修改,可以從範例看出一些端倪:

假如我們的資料庫的分類表格中存在一個欄位儲存判斷式為

學生分數 between 80 and 90

之後我們可以宣告兩個變數與執行他們

  1. declare @sqlcmd varchar(255)
  2.  
  3. declare @condition varchar(255)
  4.  
  5. declare @stundentno char(16)
  6.  
  7.  
  8.  
  9. set @sqlcmd = 'update 學生成績 set 分數分類 = ''A'' where studentno = @stundentno and '
  10.  
  11. select @condition =  condition  from 分類 // 取出分數介於80 ~ 90的狀況
  12.  
  13.  
  14.  
  15.  exec sp_executesql @sqlcmd + @condition ,N'@stundentno char(16) output',@stundentno output
  16.  
  17.  
  18.  

Related Posts:

  • 【CMD】大量刪除指令Windows下,[cmd]大量刪除指令  [delDir.bat] @echo 移動到該磁區 c: @echo 刪除資料夾下所有檔案(包含該資料夾本身) rmdir C:\temp\source\ /s/y @echo 建立資料夾 md C:\temp\source\ rmdir 指令參數 [rmdir [folder] /s /q /s : 刪除指定目錄和所有子目錄,包括任何文件。. /q … Read More
  • Java String 依照單字出現次數/頻率高的依序印出功能: 依照每個單字出現的次數,由大到小,排列印出。 假設: String oriString = "This is a book. That is a pencil" 輸出: is 出現 2 次 a 出現 2 次 This 出現 1 次 That 出現 1 次 book 出現 1 次 pencil 出現 1 次 程式碼: import java.util.regex.*; import java.util.*; public class… Read More
  • C++多重繼承下面為C++多重繼承的網路經典範例。 //程序作者:管寧 //站點:www.cndev-lab.com //所有稿件均有版權,如要轉載,請務必著名出處和作者 #include <iostream> using namespace std; class Vehicle { public:     Vehicle(int weight = 0) { Vehicle::weight = w… Read More
  • SQL Server Studio Manager 抓取錯誤訊息直接利用 PRINT使用下列函式,即可印出錯誤訊息。 USE AdventureWorks2008R2; GO -- Verify that the stored procedure does not already exist. IF OBJECT_ID ( 'usp_GetErrorInfo', 'P' ) IS NOT NULL DROP PROCEDURE usp_GetErrorInfo; GO -- Create pro… Read More
  • C/C++[微小位元][數字&數字]最近,因為面試的關係,想把一些東西弄清楚,所以做了一些測試: 以下是微小位元的測試,實際是就是二進位運算啦,考是算錯(怪怪) #include<stdio.h> int main(void){ int a = 44; int b = 55; int c = 33; int d = -20; printf("uni-micro operator: \n"); printf("a & b = %d\n"… Read More

0 意見: