【发布时间】:2019-12-03 19:31:41
【问题描述】:
我想编写一个计算字段的非空和非空条目的函数。我的问题是查询没有运行,因为 @tableName 变量在 select 语句中无法识别,我不知道为什么
create function dbo.getCount(@cod int, @columnName as varchar(20), @tableName as varchar(20))
Returns int as
Begin
--Count all filled entries
Return (select COUNT(*) from @tableName
where @columnName <> '' and @columnName is not null)
End;
go
【问题讨论】:
-
如果我无法使用某个函数来完成我尝试完成的任务,您有什么建议?存储过程?
-
“存储过程?” 是的,我现在正在写答案。
-
旁白:如果计算机可以随心所欲地决定一个变量是否包含一个值或看起来像有点的东西可能值得的代码,你将如何编写工作代码执行?
declare @Value as VarChar(10) = '2 x pi'; if ( 6.28 = @Value ) ...?select * from @Name where @Name not like 'sys.%' and @ColumnName > @ColumnValue order by @StuffThatMightHaveColumnNamesOrNumbersWithDirections;?
标签: sql-server function tsql