問(wèn)題描述
我知道我可以使用以下 sp 更改 sql 表:
I understand that I can change a sql table using the follow sp:
EXEC sp_rename 'customers', 'custs'
我將如何附加它以便新表以今天的日期作為后綴?
How would I go about appending this so that the new table has today's date as a suffix?
我嘗試了以下主題的變體,但收效甚微!
I've attempt variations on the below theme with little success!!
EXEC sp_rename 'customers', 'customers +(CONVERT(VARCHAR(8),GETDATE(),3))'
非常感謝任何幫助.
推薦答案
這聽(tīng)起來(lái)很糟糕!你應(yīng)該評(píng)估你的設(shè)計(jì),用名稱(chēng)中的日期重命名你的表格表明您將生成許多表,每個(gè)表用于不同的日期.您可以在表格中添加一個(gè)日期列并使用它來(lái)區(qū)分?jǐn)?shù)據(jù),而不是為不同的日期創(chuàng)建全新的表格.
This sounds like a very bad thing to do! you should evaluate your design, renaming your tables with dates in the names suggests that you will be spawning many tables, each for a different date. You could possibly add a date column into your table and use that to differentiate the data instead of creating completely new tables for different dates.
話(huà)雖如此,您不能將表達(dá)式作為 SQL Server 中存儲(chǔ)過(guò)程的參數(shù).通過(guò)嘗試將格式化日期連接到字符串customers",您試圖將表達(dá)式作為參數(shù)傳遞.
With that said, you can not have an expression as a parameter to a stored procedure in SQL Server. By attempting to concatenate the formatted date to the string 'customers', you were trying to pass an expression as a parameter.
您必須先將表達(dá)式存儲(chǔ)在局部變量中,然后使用該局部變量調(diào)用存儲(chǔ)過(guò)程:
you must store the expression in a local variable first, and then call the stored procedure with that local variable:
DECLARE @Value varchar(500)
SET @Value='customers' +(CONVERT(VARCHAR(8),GETDATE(),3))
EXEC sp_rename 'customers', @Value
這篇關(guān)于將 SQL 表名附加到今天的日期的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!