简体   繁体   English

使用动态SQL创建表

[英]Create table with Dynamic SQL

So I have been working a few days on this, and maybe I have been looking at it too long. 因此,我已经为此工作了几天,也许我已经考虑了太久了。

What I am trying to do is create a procedure that will create another table on based off of my SELECT query the reason is the main table is over 17 lines long and the power at be want this information broken about by a three digit ID. 我要尝试做的是创建一个过程,该过程将基于我的SELECT查询创建另一个表,原因是主表长17行以上,并且希望此信息被三位数的ID破坏。 This ID number is sequential, but I do have a table with all of them listed. 这个ID号是连续的,但我确实有一个列出所有表的表。

My problem has three(at least I hope so) parts. 我的问题包括三个部分(至少我希望如此)。

Create the dynamic SQL string to make the table and pull right information (DONE) 创建动态SQL字符串以创建表并提取正确的信息(DONE)

DECLARE @SQL NVARCHAR(MAX),  
@Class NVARCHAR(3)  
SET  @Class ='103'  
SET @SQL = 'SELECT ID.PartIntchNbr, ID.MfrCd, ID.OemId, ID.Application, ID.Interchange ,ID.HollanderPrice INTO     Hldr.Hollander'+@Class+  
' FROM [2014_EBook].dbo.HollanderInformation AS ID   
WHERE SUBSTRING(ID.Interchange, 1, 3) = '+@Class;  
EXEC (@SQL) 

This works however I am afraid there could be a safer/cleaner way to do it. 这可行,但是恐怕会有更安全/更清洁的方式来做到这一点。

I run into issue when I try to create a procedure from this so I can pass in a variable for @Class. 尝试从中创建过程时遇到问题,因此我可以为@Class传入变量。

CREATE PROCEDURE Hldr.HldrBreakDown  
@Class NVARCHAR(3)  
AS  

DECLARE   
@SQL NVARCHAR(MAX)  
SET @SQL =   
'SELECT ID.PartIntchNbr, ID.MfrCd, ID.OemId, ID.Application, ID.Interchange ,ID.HollanderPrice INTO        Hldr.Hollander'+@Class+  
 ' FROM [2014_EBook].dbo.HollanderInformation AS ID  
  WHERE SUBSTRING(ID.Interchange, 1, 3) = '+@Class  
GO  

I do not receive an error and everything runs fine but a table is not made when I call the procedure and input a variable - I will admit that I am trying to learn and much of it is by online resources with trial and error 我没有收到任何错误,并且一切运行正常,但是在调用该过程并输入变量时并没有创建表格-我承认我正在尝试学习,并且其中大部分是通过在线资源进行的,反复试验

Third - once this is all done the plan is to use Table-Valued Parameters to input my variables and let SQL churn through the night 第三-完成所有操作后,计划是使用表值参数输入我的变量,并使SQL彻夜难眠

What am a missing and what are the ways I could have cleaner and secure code? 缺少什么?我可以用哪些方式获得更干净,更安全的代码?

Thanks 谢谢

It's not clear what you are asking for, but I am going to take an educated guess that you have a table called HollanderInformation with millions of rows in it and somebody else wants to query that table based on the first 3 digits of the Interchange column. 目前尚不清楚您要的是什么,但是我要进行一个有根据的猜测,即您有一个名为HollanderInformation的表,其中包含数百万行,并且其他人希望根据Interchange列的前3位查询该表。 One more guess: querying this data takes a very long time. 还有一个猜测:查询此数据需要很长时间。

As mentioned in the comments, the powers that be may be telling you the problem they have and leave it up to you to solve, or dictating the solution. 正如评论中提到的那样,可能的权力是在告诉您他们所遇到的问题,并由您自己解决或决定解决方案。 If the solution above is what they are dictating, then read this solution and take this back to them. 如果上述解决方案是他们要求的解决方案,请阅读此解决方案并将其带回给他们。 If not, then read it and execute it. 如果没有,请阅读并执行。

SELECT
  ID.PartIntchNbr,
  ID.MfrCd,
  ID.OemId,
  ID.Application,
  ID.Interchange,
  ID.HollanderPrice
FROM [2014_EBook].dbo.HollanderInformation AS ID  
WHERE ID.Interchange LIKE @Class + '%'

Then add an index on Interchange and see if this speeds it up. 然后在Interchange上添加索引,看看是否可以加快索引速度。

create nonclustered index on ix_HollanderInformation_Interchange [2014_EBook].dbo.HollanderInformation (Interchange);

If that works out well for you, then you can easily make a table-valued function out of it which takes a @Class parameter. 如果对您来说效果很好,那么您可以轻松地使用@Class参数从中创建一个表值函数。 This is from the SQL Server technet documentation and adapted for your needs. 这来自SQL Server technet文档,并且可以根据您的需要进行调整。

IF OBJECT_ID(N'dbo.HldrBreakDown', N'IF') IS NOT NULL
    DROP FUNCTION dbo.HldrBreakDown;
GO

CREATE FUNCTION dbo.HldrBreakDown(@Class nvarchar(50))  -- whatever type is appropriate here
RETURNS table
AS
RETURN (
  SELECT
    ID.PartIntchNbr,
    ID.MfrCd,
    ID.OemId,
    ID.Application,
    ID.Interchange,
    ID.HollanderPrice
  FROM dbo.HollanderInformation AS ID  
  WHERE ID.Interchange LIKE @Class + '%'
);

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

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