在 SQL Server 2012 实例上,如何使用 SQL 查询获取 SQL Server 实例的名称?
我想显示当前安装在 SQL Server 中的实例名称。
我有一个使用 ODBC 访问 SQL Server 2012 数据库的应用程序,SQLSetStmtAttr用于将SQL_SOPT_SS_CURSOR_OPTIONS选项设置为SQL_CO_FFO. 这意味着它从数据库中读取的内容由快速只进游标支持。
这在大多数情况下是可以的,但是有时快速只进游标的性能明显低于“通常”静态游标(我们将使用 default 获得的游标SQL_CO_OFF)。
问题:有没有办法强制 SQL Server 不使用快进游标?
显然,正确的方法是更改应用程序,并且该过程正在进行中,但这需要时间,同时我正在寻找临时解决方法。
到目前为止,我唯一的想法是使用它:(来自Fast Forward-only Cursors)
Fast Forward-only 游标的隐式转换
在以下情况下,仅快进游标会隐式转换为其他游标类型:
- 如果 SELECT 语句将一个或多个表与触发器表(INSERTED/DELETED)连接起来,则游标将转换为静态游标
但是,即使是临时解决方法,这似乎也有点难看。可能有更好的方法吗?
旁注:在这种情况下,仅快速向前游标表现不佳的主要原因是它们不支持并行性。
对于非快进sys.dm_exec_cursors给我:
API | 快照 | 只读 | 全球 (0)
而快进选项是:
API | 快进| 只读 | 全球 (0)
快照选项比快进选项快 5 倍。如果我查看它们的查询计划,除了DegreeOfParallelism之外,它们没有太大不同。快照是 16 而快进是 0。问题是我们正在从视图中读取(我们没有太多控制权),而这些视图在其设计中是次优的。
如何where col % 5 = 4在 SQL Server 中重写。重写这个的原因是因为我根据这个条件从表中选择了一些列。因为 % 用于导致索引扫描而不是搜索的地方。这个你能帮我吗。
我需要在一张表中包含 2 个标识列。一个是正常身份,另一个将每年重置。
我DBCC CHECKIDENT ('TestTable', RESEED, 0)在插入程序的帮助下完成了重置部分。
但除了这个,我不知道如何拥有一个正常的身份。解决方案可能使用两个表,但我更喜欢将它们都放在一个表中。
在最后一天更新表中的列时,我们的高级 DBA 向我展示了一种减少读取和争用的新技术。
伪代码是
WHILE
[always true condition]
BEGIN
UPDATE [...]
IF @@ROWCOUNT = 0 BREAK;
END
Run Code Online (Sandbox Code Playgroud)
但是,在运行此程序时,我不小心将“包含实际执行计划”留在了“消息”选项卡中,因此我看到重复出现此情况:
(1000 row(s) affected)
(1 row(s) affected)
Run Code Online (Sandbox Code Playgroud)
但是代码有效并且 WHILE 循环有效!
我的问题是,如果包括实际执行计划返回 1 行受影响,这不应该创建一个无限循环吗?
为什么没有被计算在内?
示例表如下:
USE [tempdb];
GO
CREATE TABLE [dbo].[UpdateMePlease]
([ID] INT IDENTITY(1, 1), [UselessNote] VARCHAR(10) DEFAULT 'Delete me!', [UpdateValue] VARCHAR(10) NULL)
GO
INSERT INTO [dbo].[UpdateMePlease] ([UselessNote]) DEFAULT VALUES;
GO 10000
SELECT * FROM [dbo].[UpdateMePlease];
/* turn Actual Execution Plan on here */
WHILE 1 = 1 BEGIN
UPDATE TOP (1000) [dbo].[UpdateMePlease] …Run Code Online (Sandbox Code Playgroud) 我有一个表Vendors,其中包含一列VendorContactFname,该列以小写形式编写。
我只想大写它的第一个字母。我想在左边的第一个字母上连接一个 Upper,所有其他剩余的字母都以其原始的小写字母连接到它的右边,即与子字符串:
Upper(Left(VendorContactFname,1) ) +
Substring('VendorContactFName',2, Len('contactFname')-1 )
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
我有表 Vendors (VendorName, VendorState,....) 和 Invoices (InvoiceID, InvoiceTotal,...)。我想获得大于平均状态 InvoiceTotal 的发票(作为 InvoiceId)。
我知道我首先找到每个州的平均总数:
SELECT VendorState, Avg(InvoiceTotal) AS AvgStateInvoice
from Invoices I join Vendors V on V.VendorID= I.VendorID
group by VendorState
Run Code Online (Sandbox Code Playgroud)
所以我现在有按州列出的平均 InvoiceTotal 列表。现在我需要弄清楚:
如何进行外部查询以选择那些大于州平均水平的发票,这就是我迷路的地方,因为我不记得进行比较的语法。我想它会是这样的:
SELECT InvoiceId from Invoices where InvoiceTotal > .....?
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
有人可以解释一下为什么以下内容没有被缓存
use AdventureWorks2014
go
DECLARE @id INT=43865
SELECT
sp.[BusinessEntityID]
, so.salesorderid
FROM sales.salesorderheader so JOIN sales.salesperson sp
ON sp.[BusinessEntityID] = so.salespersonid
WHERE so.salesorderid = @id
Run Code Online (Sandbox Code Playgroud)
每次我使用不同的参数执行上述查询时,它都会在计划缓存中创建一个条目
顺便说一下,优化临时工作负载已设置
谢谢
我需要从A包含三个外键的表中找到一个值到另外两个表B和C.
为了实验,我测试了两种查询值的方法:
多个查询:
declare @start int = (select top 1 [Id] from [B] where [Day] = '2015-01-01')
declare @end int = (select top 1 [Id] from [B] where [Day] = '2017-06-14')
declare @category int = (select top 1 [Id] from [C] where [Title] = 'Hello, World!')
select top 1 [Name]
from [A]
where [StartId] = @start
and [EndId] = @end
and [CategoryId] = @category
and [Day] = '2016-05-27'
Run Code Online (Sandbox Code Playgroud)
单个查询:
select top 1 [Name]
from …Run Code Online (Sandbox Code Playgroud) performance sql-server execution-plan sql-server-2012 query-performance
I successfully created (scripted) a database in SQL Server 2012.
My problem is that while the database itself is listed in the object explorer, the tables from the database are not. The database I scripted did not contain actual data, i.e., there were no 'Insert' statements. Is this last the reason why the tables are not listed?
I tried to see if the tables were stored somehow without appearing by running an insert into statement for one of the tables …