次の問題があります: 1 つの列にテキストと数値を含む SQL テーブルがあります。テキストと数字を処理するために 2 つのビューを作成しました。
ビュー全体を選択すると期待どおりに動作しますが、select ステートメントに where ステートメントを追加すると、次のエラーが発生します。
Conversion failed when converting the varchar value 'Thomas' to data type int.
外側のselectのステートメントの前に、内側のビューのwhereステートメントを適用するようにサーバーに伝えることはできますか?
エラーを再現するには、次の SQL コードを使用してください。
create schema tst
go
create table tst.tbl(strValue varchar(10))
insert tst.tbl(strValue)
values ('Thomas'), ('1991'), ('Reto'), ('21'), ('Strub')
go
create view tst.tblStr as
select strValue
from tst.tbl
where isnumeric(strValue)=0
go
create view tst.tblInt as
select cast(strValue as int) intValue
from tst.tbl
where isnumeric(strValue)=1
go
select * from tst.tblStr order by strValue --ok
select * from tst.tblInt order by intValue --ok
go
select * from tst.tblInt where intValue<100 --not ok
go
select * from tst.tbl where isnumeric(strValue)=1 and cast(strValue as int) < 100 --ok
go
drop view tst.tblInt
drop view tst.tblStr
drop table tst.tbl
drop schema tst