如何在SQL select语句中设置列值?

时间:2016-01-06 05:26:18

标签: sql .net sql-server

我想将所有行中的一个列值设置为“0”

 SqlCommand cmd = m_objDbCAPEX.GetSqlCommand(CommandType.Text);

 cmd.CommandText = "SELECT CAPEX_BillOfMaterialItem.* FROM CAPEX_BillOfMaterialItem,CAPEX_BillOfMaterial WHERE CAPEX_BillOfMaterialItem.szBillOfMaterialId = CAPEX_BillOfMaterial.szBillOfMaterialId and CAPEX_BillOfMaterial.szProjectCode = '" + szProjectCode + "'";

 da = new SqlDataAdapter(cmd);
 da.Fill(dtDat);

2 个答案:

答案 0 :(得分:2)

您可以尝试将其设置为:

select 0 as fieldName, col1, col2,...  from yourTable

在这种情况下,您的fieldName列将设置为0.(您可以使用fieldName 替换列的实际名称)

答案 1 :(得分:0)

为@ Rahul的回答添加一个观点。

如果该字段具有特定值,并且如果您希望单独使其为0,那么您需要CASE语句。

SELECT CASE 
         WHEN capex_billofmaterialitem.fieldname = 'checking value goes here' 
       THEN 0 
         ELSE capex_billofmaterialitem.fieldname 
       END AS fieldname1, 
       capex_billofmaterialitem.* 
FROM   capex_billofmaterialitem, 
       capex_billofmaterial 
WHERE  capex_billofmaterialitem.szbillofmaterialid = 
              capex_billofmaterial.szbillofmaterialid 
       AND capex_billofmaterial.szprojectcode = '" + szProjectCode + "'