Replace text within a Query











up vote
0
down vote

favorite












I have a very long list of data that i need to insert into a table. I have a pre-formatted list of queries that does it, in which i go through each query and replace 'X' with part of an item number. Is there any way that i can save myself typing or copying the text dozens of times over by just replacing 'X' with the text I need?



My set of queries is much longer than this but looks like



    Insert into Inventory_Ingredients 
Values ('X300', 1001, '30label', 1, '0', 0)

Insert into Inventory_Ingredients
Values ('X300', 1001, '30b', 1, '0', 0)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'Shrink30', 1, '0', .50/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'recipeX', 1, '0', 1.00/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'wiznic100', 0*30/100, '0', 2.00/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'vg', 30-(select sum(inventory_ingredients.quantity)
from inventory_ingredients
where itemnum='recipeX'
group by itemnum)-(0*30/100), '0', 2.00/100)









share|improve this question


















  • 1




    how about using a variable instead of X ?
    – iSR5
    Nov 11 at 1:06















up vote
0
down vote

favorite












I have a very long list of data that i need to insert into a table. I have a pre-formatted list of queries that does it, in which i go through each query and replace 'X' with part of an item number. Is there any way that i can save myself typing or copying the text dozens of times over by just replacing 'X' with the text I need?



My set of queries is much longer than this but looks like



    Insert into Inventory_Ingredients 
Values ('X300', 1001, '30label', 1, '0', 0)

Insert into Inventory_Ingredients
Values ('X300', 1001, '30b', 1, '0', 0)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'Shrink30', 1, '0', .50/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'recipeX', 1, '0', 1.00/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'wiznic100', 0*30/100, '0', 2.00/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'vg', 30-(select sum(inventory_ingredients.quantity)
from inventory_ingredients
where itemnum='recipeX'
group by itemnum)-(0*30/100), '0', 2.00/100)









share|improve this question


















  • 1




    how about using a variable instead of X ?
    – iSR5
    Nov 11 at 1:06













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a very long list of data that i need to insert into a table. I have a pre-formatted list of queries that does it, in which i go through each query and replace 'X' with part of an item number. Is there any way that i can save myself typing or copying the text dozens of times over by just replacing 'X' with the text I need?



My set of queries is much longer than this but looks like



    Insert into Inventory_Ingredients 
Values ('X300', 1001, '30label', 1, '0', 0)

Insert into Inventory_Ingredients
Values ('X300', 1001, '30b', 1, '0', 0)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'Shrink30', 1, '0', .50/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'recipeX', 1, '0', 1.00/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'wiznic100', 0*30/100, '0', 2.00/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'vg', 30-(select sum(inventory_ingredients.quantity)
from inventory_ingredients
where itemnum='recipeX'
group by itemnum)-(0*30/100), '0', 2.00/100)









share|improve this question













I have a very long list of data that i need to insert into a table. I have a pre-formatted list of queries that does it, in which i go through each query and replace 'X' with part of an item number. Is there any way that i can save myself typing or copying the text dozens of times over by just replacing 'X' with the text I need?



My set of queries is much longer than this but looks like



    Insert into Inventory_Ingredients 
Values ('X300', 1001, '30label', 1, '0', 0)

Insert into Inventory_Ingredients
Values ('X300', 1001, '30b', 1, '0', 0)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'Shrink30', 1, '0', .50/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'recipeX', 1, '0', 1.00/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'wiznic100', 0*30/100, '0', 2.00/100)

Insert into Inventory_Ingredients
Values ('X300', 1001, 'vg', 30-(select sum(inventory_ingredients.quantity)
from inventory_ingredients
where itemnum='recipeX'
group by itemnum)-(0*30/100), '0', 2.00/100)






sql sql-server tsql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 0:30









Yofi

154




154








  • 1




    how about using a variable instead of X ?
    – iSR5
    Nov 11 at 1:06














  • 1




    how about using a variable instead of X ?
    – iSR5
    Nov 11 at 1:06








1




1




how about using a variable instead of X ?
– iSR5
Nov 11 at 1:06




how about using a variable instead of X ?
– iSR5
Nov 11 at 1:06












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Any reason dynamic SQL wouldn't work for what you're doing? Very useful in situations like this. Or if the X is just for values being inserted rather than for changing the query structure, variables would likely do the trick. Even so, I bet one of the two options will do it for you. Here's a good site for dynamic SQL help... https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/






share|improve this answer





















  • None at all, I just am very new and was previously unaware of its existence. so i can just replace all my 'X' with @X and DECLARE @X varchar(50) SET @X = 'insert_data_here'?
    – Yofi
    Nov 11 at 15:19










  • Msg 547, Level 16, State 0, Line 5 The INSERT statement conflicted with the FOREIGN KEY constraint "fkInventory_Ingredients". The conflict occurred in database "cresql", table "dbo.Inventory". The statement has been terminated.
    – Yofi
    Nov 11 at 16:03












  • When I take the '@X300' out of ticks, i get Must declare the scalar variable "@X300".
    – Yofi
    Nov 11 at 16:06










  • Presumably because it thinks that @X300 is the variable here. How do specify that only @X is the variable when it must be only part of a string? Maybe something with SUBSTRING
    – Yofi
    Nov 11 at 16:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244759%2freplace-text-within-a-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Any reason dynamic SQL wouldn't work for what you're doing? Very useful in situations like this. Or if the X is just for values being inserted rather than for changing the query structure, variables would likely do the trick. Even so, I bet one of the two options will do it for you. Here's a good site for dynamic SQL help... https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/






share|improve this answer





















  • None at all, I just am very new and was previously unaware of its existence. so i can just replace all my 'X' with @X and DECLARE @X varchar(50) SET @X = 'insert_data_here'?
    – Yofi
    Nov 11 at 15:19










  • Msg 547, Level 16, State 0, Line 5 The INSERT statement conflicted with the FOREIGN KEY constraint "fkInventory_Ingredients". The conflict occurred in database "cresql", table "dbo.Inventory". The statement has been terminated.
    – Yofi
    Nov 11 at 16:03












  • When I take the '@X300' out of ticks, i get Must declare the scalar variable "@X300".
    – Yofi
    Nov 11 at 16:06










  • Presumably because it thinks that @X300 is the variable here. How do specify that only @X is the variable when it must be only part of a string? Maybe something with SUBSTRING
    – Yofi
    Nov 11 at 16:22















up vote
0
down vote













Any reason dynamic SQL wouldn't work for what you're doing? Very useful in situations like this. Or if the X is just for values being inserted rather than for changing the query structure, variables would likely do the trick. Even so, I bet one of the two options will do it for you. Here's a good site for dynamic SQL help... https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/






share|improve this answer





















  • None at all, I just am very new and was previously unaware of its existence. so i can just replace all my 'X' with @X and DECLARE @X varchar(50) SET @X = 'insert_data_here'?
    – Yofi
    Nov 11 at 15:19










  • Msg 547, Level 16, State 0, Line 5 The INSERT statement conflicted with the FOREIGN KEY constraint "fkInventory_Ingredients". The conflict occurred in database "cresql", table "dbo.Inventory". The statement has been terminated.
    – Yofi
    Nov 11 at 16:03












  • When I take the '@X300' out of ticks, i get Must declare the scalar variable "@X300".
    – Yofi
    Nov 11 at 16:06










  • Presumably because it thinks that @X300 is the variable here. How do specify that only @X is the variable when it must be only part of a string? Maybe something with SUBSTRING
    – Yofi
    Nov 11 at 16:22













up vote
0
down vote










up vote
0
down vote









Any reason dynamic SQL wouldn't work for what you're doing? Very useful in situations like this. Or if the X is just for values being inserted rather than for changing the query structure, variables would likely do the trick. Even so, I bet one of the two options will do it for you. Here's a good site for dynamic SQL help... https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/






share|improve this answer












Any reason dynamic SQL wouldn't work for what you're doing? Very useful in situations like this. Or if the X is just for values being inserted rather than for changing the query structure, variables would likely do the trick. Even so, I bet one of the two options will do it for you. Here's a good site for dynamic SQL help... https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 2:27









Utrolig

242211




242211












  • None at all, I just am very new and was previously unaware of its existence. so i can just replace all my 'X' with @X and DECLARE @X varchar(50) SET @X = 'insert_data_here'?
    – Yofi
    Nov 11 at 15:19










  • Msg 547, Level 16, State 0, Line 5 The INSERT statement conflicted with the FOREIGN KEY constraint "fkInventory_Ingredients". The conflict occurred in database "cresql", table "dbo.Inventory". The statement has been terminated.
    – Yofi
    Nov 11 at 16:03












  • When I take the '@X300' out of ticks, i get Must declare the scalar variable "@X300".
    – Yofi
    Nov 11 at 16:06










  • Presumably because it thinks that @X300 is the variable here. How do specify that only @X is the variable when it must be only part of a string? Maybe something with SUBSTRING
    – Yofi
    Nov 11 at 16:22


















  • None at all, I just am very new and was previously unaware of its existence. so i can just replace all my 'X' with @X and DECLARE @X varchar(50) SET @X = 'insert_data_here'?
    – Yofi
    Nov 11 at 15:19










  • Msg 547, Level 16, State 0, Line 5 The INSERT statement conflicted with the FOREIGN KEY constraint "fkInventory_Ingredients". The conflict occurred in database "cresql", table "dbo.Inventory". The statement has been terminated.
    – Yofi
    Nov 11 at 16:03












  • When I take the '@X300' out of ticks, i get Must declare the scalar variable "@X300".
    – Yofi
    Nov 11 at 16:06










  • Presumably because it thinks that @X300 is the variable here. How do specify that only @X is the variable when it must be only part of a string? Maybe something with SUBSTRING
    – Yofi
    Nov 11 at 16:22
















None at all, I just am very new and was previously unaware of its existence. so i can just replace all my 'X' with @X and DECLARE @X varchar(50) SET @X = 'insert_data_here'?
– Yofi
Nov 11 at 15:19




None at all, I just am very new and was previously unaware of its existence. so i can just replace all my 'X' with @X and DECLARE @X varchar(50) SET @X = 'insert_data_here'?
– Yofi
Nov 11 at 15:19












Msg 547, Level 16, State 0, Line 5 The INSERT statement conflicted with the FOREIGN KEY constraint "fkInventory_Ingredients". The conflict occurred in database "cresql", table "dbo.Inventory". The statement has been terminated.
– Yofi
Nov 11 at 16:03






Msg 547, Level 16, State 0, Line 5 The INSERT statement conflicted with the FOREIGN KEY constraint "fkInventory_Ingredients". The conflict occurred in database "cresql", table "dbo.Inventory". The statement has been terminated.
– Yofi
Nov 11 at 16:03














When I take the '@X300' out of ticks, i get Must declare the scalar variable "@X300".
– Yofi
Nov 11 at 16:06




When I take the '@X300' out of ticks, i get Must declare the scalar variable "@X300".
– Yofi
Nov 11 at 16:06












Presumably because it thinks that @X300 is the variable here. How do specify that only @X is the variable when it must be only part of a string? Maybe something with SUBSTRING
– Yofi
Nov 11 at 16:22




Presumably because it thinks that @X300 is the variable here. How do specify that only @X is the variable when it must be only part of a string? Maybe something with SUBSTRING
– Yofi
Nov 11 at 16:22


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244759%2freplace-text-within-a-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation