如何在SQL Server 2005中获取最大ID并将其存储在int中 [英] How to get maximum ID and store it in int in SQL server 2005

查看:109
本文介绍了如何在SQL Server 2005中获取最大ID并将其存储在int中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OLTP名为FYP项目

名为FYPStarSchema的数据仓库

一旦我有了加载数据仓库,我想再次加载它但是它给出了违反主键的错误(不允许复制)



插入Product_Dimension(Product_key,Product_Name,Product_Colour,Sale_Price,Cost_Price)

SELECT P_ID,articleNO,颜色,Cur_Sale_Price,CostPrice

来自[FYP项目] .dbo.Product

从Product_Dimension中选择max(Product_key)

int a = max(Product_key )

其中Product_key> a

SELECT MAX(Product_key)AS FROM FROM Product_Dimension WHERE Product_key>最大的

select * from Product_Dimension

OLTP named FYP Project
Data Warehouse named FYPStarSchema
Once i have load data warehouse now i want to load it again but it give an error of violation of primary key(duplication cannot be allowed)

Insert Into Product_Dimension(Product_key,Product_Name,Product_Colour,Sale_Price,Cost_Price)
SELECT P_ID, articleNO, colour, Cur_Sale_Price, CostPrice
FROM [FYP Project].dbo.Product
select max(Product_key) from Product_Dimension
int a=max(Product_key)
where Product_key>a
SELECT MAX(Product_key) AS largest FROM Product_Dimension WHERE Product_key > largest
select * from Product_Dimension

推荐答案

你的查询完全没有意义 - 甚至忽略了sytax错误。它有效地归结为:

Your query makes no sense at all - even ignoring the sytax errors. What it effectivly comes down to is:
SELECT MAX(column) AS largest FROM table WHERE column > largest

这显然是愚蠢的 - 它不能返回任何大于最大值的记录,因为如果记录大于最大记录,它将是最大的!悖论,我很害怕。



如果你想返回一列中的最大值那么就这样做:

Which is clearly silly - it can''t return any records that are larger than the largest value, because if a record was larger than the largest, it would be the largest! Paradox, I''m afraid.

If you want to return the largest value in a column then just do that:

SELECT MAX(Product_Key) FROM Product_Dimension

这将返回单个值,表中最大的值。

That will return a single value, the largest in the table.


这篇关于如何在SQL Server 2005中获取最大ID并将其存储在int中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆