有人能告诉我在SQL Server中何时何地需要使用begin和end阻止?
此外,该Go关键字到底是做什么的?
在SQL Server 2005中,是否存在在SQL脚本或存储过程中声明的一次性使用或本地函数的概念?我想在我正在编写的脚本中抽象出一些复杂性,但它需要能够声明一个函数.
只是好奇.
什么是查询将向我显示完整定义,包括SQL Server表的索引和键?我想要一个纯粹的查询 - 并且知道SQL Studio可以给我这个,但我经常在"狂野"的计算机上,只有最简单的应用程序,我没有权利安装工作室.但SQLCMD始终是一种选择.
更新:我尝试过sp_help,但只生成一条显示Name,Owner,Type和Created_Datetime的记录.sp_help还有其他我缺少的东西吗?
这就是我所说的:
sp_help机场
请注意,我确实需要定义表的DDL.
我想在SQL Server 2005中创建一个列列表,其中包含标识列及其在T-SQL中的相应表.
结果将是这样的:
TableName,ColumnName
我通过编写代码而不是使用GUI在Microsoft SQL Server 2000中创建一个新表,我正在尝试学习如何"手动方式".
这是我实际使用的代码,它工作正常:
CREATE TABLE "attachments"
(
"attachment_id" INT NOT NULL,
"load_date" SMALLDATETIME NOT NULL,
"user" VARCHAR(25) NOT NULL,
"file_name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_attachments" PRIMARY KEY ("attachment_id"),
CONSTRAINT "fk_users" FOREIGN KEY ("user") REFERENCES "users" ("user"),
CONSTRAINT "ch_load_date" CHECK ("load_date" < GETDATE())
)
Run Code Online (Sandbox Code Playgroud)
我已经自己指定了主键,外键和检查约束,因为这样我可以为它们定义一个名称,否则将它们声明为内联会使SQL Server生成一个随机名称,而我并不"喜欢"它.
当我尝试声明默认值约束时出现问题:查看Internet上的信息以及Microsoft SLQ Server Management Studio如何创建它,我了解它可以内联和单独创建:
"load_date" SMALLDATETIME NOT NULL DEFAULT GETDATE()
Run Code Online (Sandbox Code Playgroud)
要么
CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
Run Code Online (Sandbox Code Playgroud)
内联方法工作正常,但它像往常一样为constaint生成一个随机名称,独立方法抛出错误,说Incorrect syntax near 'FOR'..
此外,如果我创建表然后ALTER它,该命令工作:
ALTER TABLE "attachments"
ADD CONSTRAINT "df_load_date" …Run Code Online (Sandbox Code Playgroud) 如何在SQL中为变量分配exec调用的结果?我有一个名为的存储过程up_GetBusinessDay,它返回一个日期.
你能做这样的事吗:
exec @PreviousBusinessDay = dbo.up_GetBusinessDay @Date, -1
Run Code Online (Sandbox Code Playgroud) 我有以下两个表:
Table1
----------
ID Name
1 A
2 B
3 C
Table2
----------
ID Name
1 Z
Run Code Online (Sandbox Code Playgroud)
我需要从数据插入Table1到Table2.我可以使用以下语法:
INSERT INTO Table2(Id, Name) SELECT Id, Name FROM Table1
Run Code Online (Sandbox Code Playgroud)
但是,在我的情况下,重复的ID可能存在Table2(在我的情况下,它只是" 1")并且我不想再次复制,因为这会引发错误.
我可以这样写:
IF NOT EXISTS(SELECT 1 FROM Table2 WHERE Id=1)
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1
ELSE
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1 WHERE Table1.Id<>1
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法可以不使用IF - ELSE?我想避免INSERT INTO-SELECT基于某些条件的两个陈述.
我在SQL Server中有大字符串.我想将该字符串截断为10或15个字符
原始字符串
this is test string. this is test string. this is test string. this is test string.
Run Code Online (Sandbox Code Playgroud)
期望的字符串
this is test string. this is ......
Run Code Online (Sandbox Code Playgroud) 我CONCAT在SQL Server 2008 R2中寻找一个功能.我找到了这个功能的链接.但是当我使用此函数时,它会出现以下错误:
消息195,级别15,状态10,行
7'CONCAT'不是公认的内置函数名称.
该CONCAT功能是否存在于SQL Server 2008 R2中?
如果没有,我如何连接SQL Server 2008 R2中的字符串?
我需要确保一个给定的字段在字符之间没有多个空格(我不关心所有空格,只关心空格).
所以
'single spaces only'
Run Code Online (Sandbox Code Playgroud)
需要变成
'single spaces only'
Run Code Online (Sandbox Code Playgroud)
以下不起作用
select replace('single spaces only',' ',' ')
Run Code Online (Sandbox Code Playgroud)
因为它会导致
'single spaces only'
Run Code Online (Sandbox Code Playgroud)
我更喜欢坚持使用原生T-SQL而不是基于CLR的解决方案.
思考?
t-sql ×10
sql-server ×9
sql ×3
constraints ×1
default ×1
metadata ×1
scripting ×1
sql-insert ×1