将 SQL 表名附加到今天的日期

Append SQL table name with today#39;s date(将 SQL 表名附加到今天的日期)
本文介绍了将 SQL 表名附加到今天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用以下 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))'

非常感谢任何帮助.

推荐答案

这听起来很糟糕!你应该评估你的设计,用名称中的日期重命名你的表格表明您将生成许多表,每个表用于不同的日期.您可以在表格中添加一个日期列并使用它来区分数据,而不是为不同的日期创建全新的表格.

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.

话虽如此,您不能将表达式作为 SQL Server 中存储过程的参数.通过尝试将格式化日期连接到字符串customers",您试图将表达式作为参数传递.

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.

您必须先将表达式存储在局部变量中,然后使用该局部变量调用存储过程:

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

这篇关于将 SQL 表名附加到今天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)