简体   繁体   English

你能简化这个问题的编码方式吗?

[英]Can you simplify how to code this problem?

Suppose that I work with an external company that sends me folios for an invoice every time they are due.假设我与一家外部公司合作,该公司会在每次发票到期时向我发送对开单。 For example, we started from folio 1 to 999. As we were very successful, we reached invoice 999, for which the entity gave me new billing codes ranging from 1000 to 10000. This is the base, so for this I have created a table that will store these folios as follows, idEntity (refers to the foreign external entity), folioInicio, folioTermino, folioActual.例如,我们从 folio 1 开始到 999。由于我们非常成功,我们得到了发票 999,为此实体给了我新的账单代码,范围从 1000 到 10000。这是基础,所以为此我创建了一个表将按如下方式存储这些作品集,idEntity(指外部外部实体)、folioInicio、folioTermino、folioActual。 The current folio field has the last folio used by the invoice, then through a trigger every time a product or service is invoiced, it increases and changes the field in the table.当前对开字段包含发票使用的最后一个对开,然后在每次为产品或服务开具发票时通过触发器,它会增加和更改表中的字段。

This is the base, but in reality that folio when printing it in a pdf document or in the operation is transformed into a 7-digit alphanumeric by prefixing an F. For example, folio 1 would be F000001.这是基础,但实际上,在 pdf 文档中打印时或在操作中通过前缀 F 将作品集转换为 7 位字母数字。例如,作品集 1 将是 F000001。 and so on.等等。

For this, create a function that is executed in the trigger as follows.为此,创建一个在触发器中执行的 function,如下所示。

CREATE FUNCTION dbo.GenerateFolioNumber(@FOLIO BIGINT)
RETURN VARCHAR(7)
ACE
START
DECLARE @NROFOLIO VARCHAR(7);
IF(LEN(CONVERT(VARCHAR(7), @FOLIO)) = 1) set @NROFOLIO ='F00000'+ CONVERT(VARCHAR(7), @FOLIO);
IF(LEN(CONVERT(VARCHAR(7), @FOLIO)) = 2) set @NROFOLIO ='F0000'+ CONVERT(VARCHAR(7), @FOLIO);
IF(LEN(CONVERT(VARCHAR(7), @FOLIO)) = 3) set @NROFOLIO ='F000'+CONVERT(VARCHAR(7), @FOLIO);
IF(LEN(CONVERT(VARCHAR(7), @FOLIO)) = 4) set @NROFOLIO ='F00'+CONVERT(VARCHAR(7), @FOLIO);
IF(LEN(CONVERT(VARCHAR(7), @FOLIO)) = 5) set @NROFOLIO ='F0'+CONVERT(VARCHAR(7), @FOLIO);
IF(LEN(CONVERT(VARCHAR(7), @FOLIO)) = 6) set @NROFOLIO ='F'+CONVERT(VARCHAR(7), @FOLIO);
RETURN @NROFOLIO;
END;

The thing is that so many IFs seem redundant to me, and I don't have much experience programming SQL but it seems to me that maybe it could be solved in another way, that's why I turn to this community to listen to their experience and see if there is another way to do this.问题是这么多的 IF 对我来说似乎是多余的,而且我没有太多编程经验 SQL 但在我看来也许可以用另一种方式解决,这就是为什么我求助于这个社区来听取他们的经验和看看是否有另一种方法可以做到这一点。

Perhaps concat() and right()也许 concat() 和 right()

Declare @FOLIO bigint = 27

Select 'F'+right(concat('0000000',@FOLIO),6)

Results结果

F000027

For the Function对于Function

ALTER Function dbo.GenerateFolioNumber(@FOLIO BIGINT)
Returns varchar(7)
Begin
    Return 'F'+right(concat('0000000',@FOLIO),6)
End

Lots of options here, another way you could approach this is using:这里有很多选项,另一种方法是使用:

declare @folio bigint = 123;
select Concat('F', Replicate('0', 6-Len(@folio)), @folio);

Also, you don't share how you are using the function however if in a trigger, and potentially against many rows, consider a table-valued function instead of a scalar function .此外,您不会分享您如何使用 function 但是如果在触发器中,并且可能针对许多行,请考虑表值 function而不是标量 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM