我有一个表格,我添加Datetime到一些列.我使用存储过程将值插入表中.在存储过程中,我有变量接受null以插入表中.我的问题是当我尝试在我的表列中插入一个空值时,我在列中得到1900-01-01.我该怎么办而不是这个默认值在colulmn中只插入NULL?
这是我的SP:
CREATE PROCEDURE dbo.Insert
@InserID int,
@InsertDate Datetime = null,
AS
Insert into Tables(InsertID, InsertDate)
Values(@InsertID, @InsertDate)
Run Code Online (Sandbox Code Playgroud)
我这样做是为了分配一个空值:
System.Data.SqlTypes.SqlDateTime getDate;
//set DateTime null
getDate = SqlDateTime.Null;
if (InsertDate.Text == "")
{
cmd1.Parameters["@InsertDate"].Value = getDate;
}
else
{
cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}
Run Code Online (Sandbox Code Playgroud)
添加到我的表列的值不是NULL,它是1900-01-01.
我该怎么办?
需要帮助编写查询以获取上个月的数据以及月初至今的数据.
如果今天的日期是2011年3月23日,我需要检索上个月的数据和截至今天的数据(2011年3月23日).
如果日期是2011年4月3日,则数据应包含3月份数据和截至2011年4月3日的数据.
谢谢,
Shahsra
为什么必须在一个表的列上具有主键,而另一个表的列具有外键引用.
create table D(Did int)
create table E(Eid int foreign key references D(Did))
Run Code Online (Sandbox Code Playgroud)
上面的查询给出了错误:
There are no primary or candidate keys in the referenced table 'D' that match
the referencing column list in the foreign key 'FK__E__Eid__79C80F94'.
Run Code Online (Sandbox Code Playgroud) 我正在创建一个新的Rails应用程序,它将与现有的模式一起使用.我已经获得了模式SQL,但我想创建Rails迁移以在开发中填充数据库.架构并不过分复杂,大约有20个表,但是我不想浪费时间和手动创建迁移的风险.
有没有办法在给定模式的SQL的情况下生成Rails迁移?
所以,我的Photo模型中有以下范围:
scope :best, order(:average_rating.desc)
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,事后评级被添加到模型中,因此生产应用程序有很多记录,其中average_rating为零.当我调用这个范围时,它首先返回所有的nils - 实际上它应该是相反的,nils应该是最后的(它们是尚未评级的照片).
如何将nils排序到此范围的末尾?
sql sorting ruby-on-rails ruby-on-rails-3 rails-activerecord
嘿,我有SQL编写器块.所以这就是我想要基于伪代码做的事情
int[] ids = SELECT id FROM (table1) WHERE idType = 1 -> Selecting a bunch of record ids to work with
FOR(int i = 0; i <= ids.Count(); ++i) -> loop through based on number of records retrieved
{
INSERT INTO (table2)[col1,col2,col3] SELECT col1, col2, col3 FROM (table1)
WHERE col1 = ids[i].Value AND idType = 1 -> Inserting into table based on one of the ids in the array
// More inserts based on Array ID's here
}
Run Code Online (Sandbox Code Playgroud)
这是我想要实现的想法,我理解数组在SQL中是不可能的,但我在这里列出来解释我的目标.
在Visual Studio中,有一个" 导入和导出"设置向导,您可以使用该向导自定义Visual Studio并将设置保存到.settings文件.
我们在SQL Server Management Studio中有类似的东西吗?
我经常在几个VM之间切换,配置每个SSMS是一个痛苦的问题.我希望能够将我的设置保存到文件中,然后将其导入到我的所有VM中.
谢谢.
如何添加SqlDataReader返回通用List的值?我有一个方法,我SqlDataReader用来CategoryID从Category表中获取.我想添加所有CategoryID通用列表.
这个剂量不起作用,因为它只返回一个categoryID,这是最后一个.我想将所有内容添加categoryID到列表中,然后返回它们.
我怎么做?
SqlConnection connection = null;
SqlDataReader reader = null;
SqlCommand cmd = null;
try
{
connection = new SqlConnection(connectionString);
cmd = new SqlCommand("select CategoryID from Categories", connection );
connection.Open();
List<int> catID = new List<int>();
dr = cmd.ExecuteReader();
while (dr.Read())
{
catID.Add(Convert.ToInt32(dr["CategoryID"].ToString()));
}
}
finally
{
if (connection != null)
connection.Close();
}
return catID;
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用"."编写列名.没有成功
样品:
SELECT PrmTable.Value = MAX(Value)
FROM TempTable
Run Code Online (Sandbox Code Playgroud)
要么
SELECT MAX(Value) AS PrmTable.Value
FROM TempTable
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
sql ×10
c# ×2
sql-server ×2
t-sql ×2
database ×1
foreign-keys ×1
insert ×1
key ×1
loops ×1
primary-key ×1
sorting ×1
sqlite ×1
ssms ×1