sql 游戏~四数,+-*/~得另一数--实用版(原创)

CREATE FUNCTION f_ww(@var varchar(50),@vs varchar(50))
RETURNS char(1)
AS
begin
 if len(@var)<>len(@vs)
  RETURN 'F'
 declare @str varchar(50),@char varchar(10)
 declare @varcharint int,@vscharint int
 declare @varstr varchar(50),@vsstr varchar(50)
 set @str=@var+','
 while charindex(',',@str)>0
 begin
  select @char=substring(@str,0,charindex(',',@str)),
   @str=substring(@str,charindex(',',@str)+1,len(@str)-charindex(',',@str)),
   @varcharint=0,@vscharint=0,
   @varstr=','+@var+',', @vsstr=','+@vs+','
  while(charindex(','+@char+',',@varstr))>0
  begin
   select @varcharint=@varcharint+1,
    @varstr=substring(@varstr,charindex(','+@char+',',@varstr)+1+len(@char),len(@varstr))
  end
 
  while(charindex(','+@char+',',@vsstr))>0
  begin
   select @vscharint=@vscharint+1,
    @vsstr=substring(@vsstr,charindex(','+@char+',',@vsstr)+1+len(@char),len(@vsstr))
  end
 if(@varcharint<>@vscharint)
  RETURN 'F'
 end
 RETURN 'T'
end
go


create proc p_ww
@int1 money,
@int2 money,
@int3 money,
@int4 money,
@result dec(18,2)
as
begin
set nocount on
select DISTINCT a.id [aid],b.id [bid],c.id [cid],d.id [did] into #wei
 from (select @int1 [id]
  union all select @int2
  union all select @int3
  union all select @int4) a,(select @int1 [id]
  union all select @int2
  union all select @int3
  union all select @int4) b,(select @int1 [id]
  union all select @int2
  union all select @int3
  union all select @int4) c,(select @int1 [id]
  union all select @int2
  union all select @int3
  union all select @int4) d
where dbo.f_ww(cast(a.id as varchar(10))+','+ cast(b.id as varchar(10))+','+ cast(c.id as varchar(10))+','+ cast(d.id as varchar(10)),
cast(@int1 as varchar(10))+','+cast(@int2 as varchar(10))+','+cast(@int3 as varchar(10))+','+cast(@int4 as varchar(10)))='T'

create table #show(mula varchar(100),result money)

declare @count1 varchar(10),@count2 varchar(10),@count3 varchar(10)
declare @operator1 varchar(2),@operator2 varchar(2),@operator3 varchar(2)
set @count1='+,-,*,/,'

while(charindex(',',@count1)>0)
begin
select @operator1=substring(@count1,0,charindex(',',@count1)),
 @count1=substring(@count1,charindex(',',@count1)+1,len(@count1))
set @count2='+,-,*,/,'

 while(charindex(',',@count2)>0)
 begin
 select @operator2=substring(@count2,0,charindex(',',@count2)),
  @count2=substring(@count2,charindex(',',@count2)+1,len(@count2))
 set @count3='+,-,*,/,'
 
  while(charindex(',',@count3)>0)
  begin
   select @operator3=substring(@count3,0,charindex(',',@count3)),
    @count3=substring(@count3,charindex(',',@count3)+1,len(@count3))
declare @sql nvarchar(4000)
--a/(b/(c/d))
select @sql='insert into #show select replace(cast(aid as varchar(10))+@stroperator1+''(''+cast(bid as varchar(10))+@stroperator2+''(''+cast(cid as varchar(10))+@stroperator3+cast(did as varchar(10))+''))'',''.00'',''''),
aid'+@operator1+'(bid'+@operator2+'(cid'+@operator3+'did))
from #wei where case when @stroperator3=''/''and did=0 then null
when @stroperator2=''/''and cid'+@operator3+'did=0 then null
when @stroperator1=''/''and bid'+@operator2+'(cid'+@operator3+'did)=0 then null
else aid'+@operator1+'(bid'+@operator2+'(cid'+@operator3+'did)) end='+rtrim(@result)
,@sql=@sql+char(10)+ --a/((b/c)/d)
'insert into #show select replace(cast(aid as varchar(10))+@stroperator1+''((''+cast(bid as varchar(10))+@stroperator2+cast(cid as varchar(10))+'')''+@stroperator3+cast(did as varchar(10))+'')'',''.00'',''''),
aid'+@operator1+'((bid'+@operator2+'cid)'+@operator3+'did)
from #wei where case when @stroperator3=''/''and did=0 then null
when @stroperator2=''/''and cid=0 then null
when @stroperator1=''/''and ((bid'+@operator2+'cid)'+@operator3+'did)=0 then null
else aid'+@operator1+'((bid'+@operator2+'cid)'+@operator3+'did) end='+rtrim(@result)
,@sql=@sql+char(10)+ --((a/b)/c)/d
'insert into #show select replace(''((''+cast(aid as varchar(10))+@stroperator1+cast(bid as varchar(10))+'')''+@stroperator2+cast(cid as varchar(10))+'')''+@stroperator3+cast(did as varchar(10)),''.00'',''''),
((aid'+@operator1+'bid)'+@operator1+'cid)'+@operator1+'did
from #wei where case when @stroperator3=''/''and did=0 then null
when @stroperator2=''/''and cid=0 then null
when @stroperator1=''/''and ((bid'+@operator2+'cid)'+@operator3+'did)=0 then null
else ((aid'+@operator1+'bid)'+@operator1+'cid)'+@operator1+'did end='+rtrim(@result)
,@sql=@sql+char(10)+ --(a/(b/c))/d
'insert into #show select replace(''(''+cast(aid as varchar(10))+@stroperator1+''(''+cast(bid as varchar(10))+@stroperator2+cast(cid as varchar(10))+''))''+@stroperator3+cast(did as varchar(10)),''.00'',''''),
(aid'+@operator1+'(bid'+@operator2+'cid))'+@operator3+'did
from #wei where case when @stroperator3=''/''and did=0 then null
when @stroperator2=''/''and cid=0 then null
when @stroperator1=''/''and bid'+@operator2+'cid=0 then null
else (aid'+@operator1+'(bid'+@operator2+'cid))'+@operator3+'did end='+rtrim(@result)
,@sql=@sql+char(10)+ --(a/b)/(c/d)
'insert into #show select replace(''(''+cast(aid as varchar(10))+@stroperator1+cast(bid as varchar(10))+'')''+@stroperator2+''(''+cast(cid as varchar(10))+@stroperator3+cast(did as varchar(10))+'')'',''.00'',''''),(aid'+@operator1+'bid)'+@operator2+'(cid'+@operator3+'did)
from #wei where case when @stroperator3=''/''and did=0 then null
when @stroperator2=''/''and cid'+@operator3+'did=0 then null
when @stroperator1=''/''and did=0 then null
else (aid'+@operator1+'bid)'+@operator2+'(cid'+@operator3+'did) end='+rtrim(@result)

exec sp_executesql
@sql
,N'@stroperator1 varchar(2),@stroperator2 varchar(2),@stroperator3 varchar(2)'
,@stroperator1=@operator1,@stroperator2=@operator2,@stroperator3=@operator3
  end
 end
end

select * from #show
drop table #show
drop table #wei
end

----------------------------
--测试
exec p_ww 1,1,1,13,24
exec p_ww 1,0.6,8,0.8,24
exec p_ww 1,0.6,0.8,6,24

 

 


drop FUNCTION dbo.f_ww
drop proc p_ww 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值