我想要使用这段代码来添加唯一标识符,但我似乎无法让它正常工作。以下是代码以及当前输出和所需输出。
Begin
DECLARE @StartDate DATETIME,
@EndDate DATETIME,
@MonthList as Varchar(50),
@NewLeaseID as int,
@LeaseID as int,
@PropertyID as int,
@Amount as int,
@ExpectedID as int
SELECT
@StartDate = '20100501'
,@EndDate = '20100801'
,@leaseID = 6,
@PropertyID = 12,
@Amount = 600,
@ExpectedID = (SELECT ISNULL(MAX(ExpectedPaymentID) + 1, 1) FROM Payments_ExpectedPayments)
INSERT INTO Payments_ExpectedPayments(ExpectedPaymentID, Amount, PropertyID, LeaseID, Month)
SELECT
@ExpectedID as ExpectedPaymentID,
(x.number + 1) * @Amount as Amount,
@PropertyID as PropertyID,
@leaseID as Leaseid,
DATENAME(MONTH, DATEADD(MONTH, x.number, @StartDate)) AS Month
FROM
master.dbo.spt_values x
WHERE
x.type = 'P'
AND x.number <= DATEDIFF(MONTH, @StartDate, @EndDate);
End
想要输出:
ExpecedID PropertyID LeaseID Month Amount
1 12 13 Jan 600
2 12 13 Feb 1200
3 12 13 March 1800
4 12 13 April 2400
我目前得到的输出:
ExpecedID PropertyID LeaseID Month Amount
1 12 13 Jan 600
1 12 13 Feb 1200
1 12 13 March 1800
1 12 13 April 2400
答案 0 :(得分:3)
您有两种选择:
ExpectedId
的标识列。只需将CREATE TABLE
从Expected int
更改为Expected int identity(1,1)
。ROW_NUMBER()
INSERT
上使用ROW_NUMBER() OVER(ORDER BY x.number) as ExpectedId
: <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/admin-servlet.xml</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>student</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>*.admin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>student</servlet-name>
<url-pattern>*.student</url-pattern>
</servlet-mapping>
</web-app>
答案 1 :(得分:0)
这是失败的,因为你在select语句之前设置了一次@ExpectedID,因此你得到了所有相同的值。
尝试在SELECT语句中使用以下行替换@ExpectedID参数。
(SELECT ISNULL(MAX(ExpectedPaymentID) + 1, 1) FROM Payments_ExpectedPayments) as ExpectedPaymentID
这将每行执行一次select语句,我想这应该可行。