SQL语句
1.需要多个字段组成一列:
convert(你要的type,列名+列名+…)其中列名之间可以夹杂各种标点文字
2.日期型的列,条件必须加‘’,否则查不到数
3.多个表进行left join的时候,千万注意on的条件(一般都用主键或者类似主键功能的列),如果左表on条件列不唯一,则容易出现类似于笛卡尔积的结果,造成多余的错误数据出现
存储过程
1.发现有些随着数据产生时间而生成的表,当无数据的时候,表不会被创建,此时存储过程会报错,因为表不存在,如果这个存储过程是多次调用,那么会影响本次所有查询集。
结局办法:新建一个函数,用来判断表名是否存在,不存在直接return。
USE [KJWEB]
GO
/****** Object: UserDefinedFunction [dbo].[TableExists] Script Date: 2022/10/28 14:02:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: wang
-- Create date: 2022年10月28日
-- Description: <Description, ,>
-- =============================================
ALTER FUNCTION [dbo].[TableExists]
(
-- Add the parameters for the function here
@TableName nvarchar(50)
)
RETURNS bit
AS
BEGIN
-- Declare the return variable here
DECLARE @Result bit
set @Result = '0'
-- Add the T-SQL statements to compute the return value here
if exists (select * from dbo.sysobjects where id = object_id(@TableName) and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
-- Return the result of the function
set @Result = '1'
RETURN @Result
end
RETURN @Result
END
对应存储过程中:
` declare @realtable varchar(500)
set @MineName = (select MineName from M_Mine where MineID =@MineID)
set @realtable = 'M_RealValue_'+@MineName+'_' + convert(varchar(6),@BeginTime,112);
if([dbo].[TableExists](@realtable)='1')
begin
-- Add your code here
end
C#中
1.datatable执行select返回datarow[],我想要datatable怎么办?
private DataTable GetNewDataTable(DataTable dt,string condition)
{
DataTable newdt = new DataTable();
newdt=dt.Clone();
DataRow[] dr = dt.Select(condition);
for(int i=0;i<dr.Length;i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
return newdt; //返回的查询结果
}
2.SQL数据库中bit数据类型在C#中用dataReader
bool.Parse( dataReader["IsCalibration"].ToString())
bit是bool型,虽然有时候在数据库里显示时0和1