简体   繁体   中英

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. This ID number is sequential, but I do have a table with all of them listed.

My problem has three(at least I hope so) parts.

Create the dynamic SQL string to make the table and pull right information (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.

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

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. 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.

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. This is from the SQL Server technet documentation and adapted for your needs.

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 + '%'
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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