所有这些都有效:
CREATE DATABASE [¯\_(?)_/¯];
GO
USE [¯\_(?)_/¯];
GO
CREATE SCHEMA [¯\_(?)_/¯];
GO
CREATE TABLE [¯\_(?)_/¯].[¯\_(?)_/¯]([¯\_(?)_/¯] NVARCHAR(20));
GO
CREATE UNIQUE CLUSTERED INDEX [¯\_(?)_/¯] ON [¯\_(?)_/¯].[¯\_(?)_/¯]([¯\_(?)_/¯]);
GO
INSERT INTO [¯\_(?)_/¯].[¯\_(?)_/¯]([¯\_(?)_/¯]) VALUES (N'[¯\_(?)_/¯]');
GO
CREATE VIEW [¯\_(?)_/¯].[vw_¯\_(?)_/¯] AS SELECT [¯\_(?)_/¯] FROM [¯\_(?)_/¯].[¯\_(?)_/¯];
GO
CREATE PROC [¯\_(?)_/¯].[sp_¯\_(?)_/¯] @Shrug NVARCHAR(20) AS SELECT [¯\_(?)_/¯] FROM [¯\_(?)_/¯].[vw_¯\_(?)_/¯] WHERE [¯\_(?)_/¯] = @Shrug;
GO
EXEC [¯\_(?)_/¯].[¯\_(?)_/¯].[sp_¯\_(?)_/¯] @Shrug = N'[¯\_(?)_/¯]';
GO
Run Code Online (Sandbox Code Playgroud)
但是您可能会看到我的意思:我不想要 @Shrug,我想要@¯\_(?)_/¯.
这些都不适用于 2008-2017 的任何版本:
CREATE PROC [¯\_(?)_/¯].[sp_¯\_(?)_/¯] @[¯\_(?)_/¯] NVARCHAR(20) AS SELECT [¯\_(?)_/¯] FROM [¯\_(?)_/¯].[vw_¯\_(?)_/¯] WHERE [¯\_(?)_/¯] …Run Code Online (Sandbox Code Playgroud) 是否可以将表值参数用作存储过程的输出参数?
这是,我想在代码中做什么
/*First I create MY type */
CREATE TYPE typ_test AS TABLE
(
id int not null
,name varchar(50) not null
,value varchar(50) not null
PRIMARY KEY (id)
)
GO
--Now I want to create stored procedu whic is going to send output type I created,
--But it looks like it is inpossible, at least in SQL2008
create PROCEDURE [dbo].sp_test
@od datetime
,@do datetime
,@poruka varchar(Max) output
,@iznos money output
,@racun_stavke dbo.typ_test READONLY --Can I Change READONLY with OUTPUT …Run Code Online (Sandbox Code Playgroud) 根据此博客,函数或存储过程的参数如果不是OUTPUT参数,则本质上是按值传递的,如果它们是参数,则基本上被视为按引用传递的更安全版本OUTPUT。
起初我认为强制声明 TVP 的目的READONLY是向开发人员明确表示 TVP 不能用作OUTPUT参数,但必须有更多的进展,因为我们不能将非 TVP 声明为READONLY. 例如以下失败:
create procedure [dbo].[test]
@a int readonly
as
select @a
Run Code Online (Sandbox Code Playgroud)
消息 346,级别 15,状态 1,过程测试
参数“@a”不能声明为 READONLY,因为它不是表值参数。
OUTPUT出于某种原因不希望 TVP 成为参数有关?鉴于当前 Postgres 9.4 中的此设置(来自此相关问题):
CREATE TABLE foo (ts, foo) AS
VALUES (1, 'A') -- int, text
, (7, 'B');
CREATE TABLE bar (ts, bar) AS
VALUES (3, 'C')
, (5, 'D')
, (9, 'E');
Run Code Online (Sandbox Code Playgroud)
db<> fiddle here(也来自上一个问题)。
我SELECT用 a写了一个FULL JOIN来实现引用问题的目标。简化:
SELECT ts, f.foo, b.bar
FROM foo f
FULL JOIN bar b USING (ts);Run Code Online (Sandbox Code Playgroud)
As per specifications, the correct way to address the column ts is without table qualification. Either of the …
是什么导致将对象名称传递给系统存储过程是合法的sp_helptext?
什么机制将对象名称转换为字符串?
例如
-- works
sp_helptext myproc
sp_helptext [myproc]
sp_helptext [dbo.myproc]
-- and behaves the same as a string
sp_helptext 'myproc'
sp_helptext 'dbo.myproc'
-- does not work
sp_helptext dbo.myproc -- Msg 102, Level 15, State 1, Line 1 incorrect syntax near '.'
-- an additional case that does not work.
sp_helptext [dbo].[myproc] -- Msg 102, Level 15, State 1, Line 1 incorrect syntax
Run Code Online (Sandbox Code Playgroud)
我不需要单引号有效的.过程名称似乎很奇怪,除非它具有分隔模式名称和过程名称。我正在寻找有关如何将其从带引号的名称自动转换为要作为参数值传递的字符串文字的解释。
我没有要解决的具体问题;我只是对没有记录的事情感到好奇。
sql-server stored-procedures syntax parameter database-internals
PLS-00306: 调用 'string' 时参数的数量或类型错误
原因:当命名的子程序调用无法与该子程序名称的任何声明匹配时,就会发生此错误。子程序名称可能拼写错误,参数可能具有错误的数据类型,声明可能有误,或者声明可能在块结构中的位置不正确。例如,如果使用拼写错误的名称或错误数据类型的参数调用内置平方根函数 SQRT,则会发生此错误。
行动:检查子程序名称的拼写和声明。还要确认它的调用是正确的,它的参数是正确的数据类型,如果它不是一个内置函数,它的声明是否正确地放置在块结构中。
如何快速识别错误的论点?
我有一个包含数十个参数的存储过程。有没有一种简单的方法来检查使用的和定义的程序之间的差异?我不想逐行检查它..
我们有一个大型(10,000 多行)程序,通常在 0.5-6.0 秒内运行,具体取决于它必须处理的数据量。在过去一个月左右的时间里,在我们使用 FULLSCAN 进行统计更新后,它开始需要 30 多秒的时间。当它变慢时,sp_recompile 会“修复”该问题,直到夜间统计作业再次运行。
通过比较慢速和快速执行计划,我将范围缩小到特定的表/索引。当它运行缓慢时,它估计将从特定索引返回约 300 行,当它运行得快时,它估计将返回 1 行。当它运行缓慢时,它在对索引进行查找后使用表假脱机,当它运行快时它不执行表假脱机。
使用 DBSS SHOW_STATISTICS,我在 excel 中绘制了索引直方图。我通常希望图表更像“起伏的山丘”,但相反,它看起来像一座山,最高点比图表上的大多数其他值高 2 到 3 倍。

如果我更新它的统计数据,没有 FULLSCAN,它看起来更正常。如果我然后再次使用 FULLSCAN 运行它,它看起来就像我上面描述的那样。
这感觉像是一个参数嗅探问题,特别与上面(看似)奇怪的索引分布有关。
proc 接受一个表值参数,参数嗅探可以发生在表值参数上吗?
编辑:proc 还需要 12 个其他参数,其中一些是可选的,其中两个是开始日期和结束日期。
直方图是奇怪的,还是我叫错了树?
我当然愿意尝试调整查询和/或尝试调整我的索引。如果这是一个很好的解决方案,那么我的问题更多是关于偏斜直方图。
我应该提到这是一个 PK IDENTITY 聚集索引。我们有两个相互通信的系统,一个是遗留系统,一个是新的本土系统。两个系统都存储相似的数据。为了使它们保持同步,当向旧系统添加事物时,新系统中此表上的 PK 会增加,即使数据没有过来(已完成 RESEED)。因此,此列中的编号可能存在一些空白。记录很少被删除,如果有的话。
任何想法将不胜感激。我很高兴收集/包含更多信息。
sql-server sql-server-2008-r2 parameter index-statistics index-tuning
我正在尝试json_extract_path_text()使用citext模块制作不区分大小写的版本。
我希望这是一个围绕内置函数的简单包装器,唯一的区别是它接受citext作为第一个参数而不是json. 我希望这是对本机实现的直接传递,只需事先进行类型转换。这是我到目前为止所拥有的:
create extension citext;
create or replace function json_extract_path_text ( string citext, variadic params text[]) RETURNS text IMMUTABLE AS
$$
BEGIN
SELECT json_extract_path_text(string::json, params);
END;
$$
LANGUAGE 'plpgsql';
Run Code Online (Sandbox Code Playgroud)
但是,由于类型不匹配,这不能正常工作:
Run Code Online (Sandbox Code Playgroud)ERROR: function json_extract_path_text(json, text[]) does not exist LINE 1: SELECT json_extract_path_text(string::json, params) ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. QUERY: SELECT json_extract_path_text(string::json, params) CONTEXT: PL/pgSQL function json_extract_path_text(citext,text[]) line 3 …
我想知道这在 Postgres 中是否可行:
最好用一个人为的例子来解释:
create or replace function test_function(filter_param1 varchar default null
, filter_param2 varchar default null)
returns integer as
$$
declare
stmt text;
args varchar[];
wher varchar[];
retid integer;
begin
if filter_param1 is not null then
array_append(args, filter_param1);
array_append(wher, 'parameter_name = $1');
end if;
if filter_param2 is not null then
array_append(args, filter_param2);
array_append(wher, 'parameter_name = $2');
end if;
stmt := 'select id from mytable where ' || array_to_string(wher, ' or ');
execute stmt into retid using args;
return retid; …Run Code Online (Sandbox Code Playgroud) 我有两个存储过程。这个速度非常快(约 2 秒)
CREATE PROCEDURE [schema].[Test_fast]
@week date
AS
BEGIN
declare @myweek date = @week
select distinct serial
from [schema].[tEventlog] as e
join [schema].tEventlogSourceName as s on s.ID = e.FKSourceName
where s.SourceName = 'source_name'
and (e.EventCode = 1 or e.EventCode = 9)
and cast(@myweek as datetime2(3)) <= [Date]
and [Date] < dateadd(day, 7, cast(@myweek as datetime2(3)))
END
Run Code Online (Sandbox Code Playgroud)
而这个运行缓慢(~ 2 小时):
create PROCEDURE [schema].[Test_slow]
@week date
AS
BEGIN
select distinct serial
from [schema].[tEventlog] as e
join [schema].tEventlogSourceName as s on s.ID …Run Code Online (Sandbox Code Playgroud) performance sql-server stored-procedures execution-plan parameter query-performance
parameter ×10
sql-server ×5
postgresql ×3
datatypes ×2
plpgsql ×2
t-sql ×2
array ×1
dynamic-sql ×1
functions ×1
index-tuning ×1
join ×1
oracle ×1
performance ×1
plsql ×1
syntax ×1
unicode ×1