希望有人可以提供帮助 - 我是新手SQL黑客(确实非常糟糕!)
我在SQL Server 2005表1和表2上有两个表:
表格1
COL1 COL2
1 10
2 20
3 30
4 10
4 20
5 20
6 30
7 10
7 20
Run Code Online (Sandbox Code Playgroud)
TABLE2
COL1 COL2
10 A
20 B
30 C
Run Code Online (Sandbox Code Playgroud)
TABLE2中的COL2是COL2 TABLE1中数值数据的字符表示.我希望这是可以理解的吗?
我已经研究了如何从TABLE1中选择COL1和COL2并连接结果以显示:
COL1 COL2Concat
1 10
2 20
3 30
4 10, 20
5 20
6 30
7 10, 20, 30
Run Code Online (Sandbox Code Playgroud)
使用这个:
SELECT COL1,
STUFF(( SELECT ',' + CAST(a.COL2 AS VARCHAR(255)) AS [text()]
FROM TABLE1 a
WHERE a.COL1 = b.COL1
ORDER BY a.COL2 …Run Code Online (Sandbox Code Playgroud) 我需要编写一个SQL Select语句来选择DateCreated在过去4周的周末所有记录.我知道我可以通过对下面的选择进行硬编码来做到这一点.但我想知道使用T-sql日期函数是否有更简单的方法.
SELECT * FROM audit_table
WHERE DateCreated BETWEEN '07-31-2010 00:00:00' AND '08-01-2010 23:59:59'
OR DateCreated BETWEEN '07-24-2010 00:00:00' AND '07-25-2010 23:59:59'
OR DateCreated BETWEEN '07-17-2010 00:00:00' AND '07-18-2010 23:59:59'
OR DateCreated BETWEEN '07-10-2010 00:00:00' AND '07-11-2010 23:59:59'
Run Code Online (Sandbox Code Playgroud) 如果我放置DISTINCT关键字我得到一个错误,否则它工作正常.
错误:消息145,级别15,状态1,过程SP_Products_GetList,第15行ORDER BY项目必须出现在选择列表中,如果指定了SELECT DISTINCT.
ALTER PROCEDURE [dbo].[SP_Products_GetList]
@CatID int,
@CatName int,
@IsNew bit,
@InActive bit,
@SortBy varchar(50),
@SortType varchar(50)
AS
SELECT DISTINCT Products.ProductID, ProductName, MAX(Price) Price, PriceID, [Description], Size, IsNew, InActive FROM (SELECT * FROM Products WHERE (@InActive is null or @InActive = InActive ) AND ( @IsNew is null or @IsNew = IsNew )) Products
INNER JOIN ProductCategory
on Products.ProductID = ProductCategory.ProductID
INNER JOIN (
SELECT * FROM Categories
WHERE
( @CatID is null or @CatID = CatID ) …Run Code Online (Sandbox Code Playgroud) 我对SQL很新,所以不确定这是否只是一个SQL编译器的事情.当我在通过JAVA或PHP之前使用SQL时,从不直接使用SQL.我正在使用SQL Server 2005并尝试向我的表添加一个列,然后填充它.这就是我现在拥有的:
ALTER TABLE House DROP COLUMN CustomerType
ALTER TABLE House ADD CustomerType char(11)
UPDATE House
SET CustomerType = CASE
WHEN ... THEN...
WHEN ... THEN...
ELSE "Other"
END
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译它时,错误,因为UPDATE函数中的CustomerType尚未定义.有没有办法运行它,以便它将编译并添加列并更新它,或者我必须在多次执行中运行它?
谢谢!
I was asked in the interview tell me the different ways of exporting database from one sql server to another, I knew only about creating a .bak file and then restoring it to another sql server which I told them. However, they asked me about a single SQL INSERT command which will perform this task.
I have googled it and can not find it. Please tell me if there is any such command ?
我的一个varchar表中有一个列,其数据如下:
1234abc
1234abcde456757
1234abc Supervisor
1234abc456 Administrator
Run Code Online (Sandbox Code Playgroud)
我想通过删除紧随其后的任何字母和数字来"清理它",所以对于我想要的上述示例:
1234
1234
1234 Supervisor
1234 Administrator
Run Code Online (Sandbox Code Playgroud)
换句话说,我想保留初始数字和最后一个字.我使用的是SUBSTRING和CHARINDEX,但这些功能去掉一切,直到字符串的结尾,我不知道我需要删除部分的长度.
有什么建议?
谢谢
当我尝试从C#代码执行下面的存储过程时,我得到一个SQLException"操作数类型碰撞:int与uniqueidentifier不兼容".
create proc sp_Get_Allfields_Account_Public
@username varchar(20),
@password varchar(20),
@acc_num UniqueIdentifier out,
@cust_name varchar(20) out,
@balance float out
as
select @acc_num=acc_num,@cust_name=cust_name,@balance=balance from Account_Public where username=@username and password=@password
Run Code Online (Sandbox Code Playgroud)
C#代码如下
cmd = new SqlCommand("sp_Get_Allfields_Account_Public", con);
cmd.CommandType = CommandType.StoredProcedure;
// Add Input Parameters
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
// Add output parameters
cmd.Parameters.AddWithValue("@acc_num", SqlDbType.UniqueIdentifier);
cmd.Parameters["@acc_num"].Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue("@cust_name", SqlDbType.VarChar);
cmd.Parameters["@cust_name"].Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue("@balance", SqlDbType.Float);
cmd.Parameters["@balance"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
表定义
create table Account_Public
(
acc_num uniqueidentifier,
cust_name varchar(20),
username varchar(20),
password varchar(20),
balance float
)
Run Code Online (Sandbox Code Playgroud) 我已在我的本地计算机上配置了log4net并正常工作,但是当我部署到我的主机(godaddy)时,它会无声地失败.我在我的开发机器和主机上使用相同的数据库/配置文件.我的log4net引用设置为copy local,而log4net.dll,.pdb和.xml存在于主机上的bin中.这是一个asp.net mvc应用程序.
编辑:没有抛出异常,应用程序按预期运行(减去日志记录)
这是在SQL Server 2005上运行的.nethost是IIS 7
我的配置的突出细节是:
<root>
<level value="DEBUG" />
<appender-ref ref="AdoNetAppender" />
</root>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
Run Code Online (Sandbox Code Playgroud)
有人对要检查的事情有任何想法吗?
我已经阅读了很多关于在SQL Server中定义自定义错误消息的内容(我正在使用2005) - 使用sp_addmessage,选择一个超过50,000的数字等等.但是我如何查询以查看已经为a定义的自定义消息数据库?TIA!
我在MS-SQL 2005数据库中有一个存储过程:
SP采用两个参数,然后在第一个查询中使用.
当我为给定的一组参数运行SP时,执行需要3分钟.
当我以常规T-SQL批处理(预先声明和设置参数)执行SP 的内容时,需要10秒.这些数字在多个连续运行中是一致的.
这是一个巨大的差异,并没有明显的功能变化.可能是什么导致了这个?
重新索引我的表(DBCC REINDEX)大大加快了SP版本的速度.SP版本现在需要1秒,而原始SQL需要6秒.
这对解决眼前的问题很有帮助,但我仍然想知道"为什么".
sql-server-2005 ×10
sql-server ×5
sql ×4
t-sql ×4
alter-table ×1
asp.net-mvc ×1
c# ×1
distinct ×1
log4net ×1
performance ×1
sql-order-by ×1
string ×1