Question

In: Computer Science

SQL Code: Write a script that creates and calls a stored procedure named test. This procedure...

SQL Code:

Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this:

2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3 1 2 9 1 3 1 1 . . .

Hint: To get this to work, you will need to nest one loop within another loop. In addition, you will need to code an IF statement within the inner loop.

Solutions

Expert Solution

SQL code screenshot (Create Stored Procedure test):

SQL code (Create Stored Procedure test):


CREATE PROCEDURE test
AS
        DECLARE @primeValue INT = 3, @range INT = 100
        DECLARE @value INT, @result VARCHAR(MAX) = '2'
        DECLARE @flag BIT

        -- This loop will iterate from 3 to 100
        WHILE @primeValue <= @range
        BEGIN
                SET @value = SQRT(@primeValue)
                SET @flag = 0
                
                -- This loop will check if current number is prime or not
                WHILE @value >= 2
                BEGIN
                        -- If divisible by the value then not a prime number
                        IF @primeValue % @value = 0
                        BEGIN
                                SET @flag = 1
                                BREAK
                        END
                        SET @value = @value - 1;
                END

                -- Adds the prime number to the result with '1' as a seperator
                IF @flag = 0
                BEGIN
                        SET @result = @result + '1' + CAST(@primeValue AS VARCHAR(3));
                END
                
                -- Getting next value
                SET @primeValue = @primeValue + 1;
        END


        -- Below code will add space in between each character
        
        -- location where we want first space 
        DECLARE @pos INT = 2 
        -- to seperate each character with a space
        DECLARE @separator nvarchar(5) = ' '
    WHILE @pos < LEN(@result)+1 
    BEGIN 
                -- Inserts a space at the given position & re-assigns the result
        SET @result = STUFF(@result, @pos, 0, @separator); 
        SET @pos = @pos+2; 
    END 

    PRINT @result;

SQL code (Call Stored Procedure test): Below code to execute the stored procedure:

EXEC test;

Result:


Related Solutions

Write a script that creates and calls a stored procedure named test. This procedure should identify...
Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3 1 2...
SQL Homework -- This script creates the schema named mgs -- Connect as the user named...
SQL Homework -- This script creates the schema named mgs -- Connect as the user named mgs --CONNECT cs270226mgs/mgs; -- Use an anonymous PL/SQL script to -- drop all tables and sequences in the current schema and -- suppress any error messages that may displayed -- if these objects don't exist BEGIN EXECUTE IMMEDIATE 'DROP SEQUENCE category_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE product_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE customer_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE address_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE order_id_seq'; EXECUTE IMMEDIATE 'DROP...
SQL stored procedures Create 2 stored procedures: sp_AddNewUser and sp_AddNewRole. Use the ManyToMany script as your...
SQL stored procedures Create 2 stored procedures: sp_AddNewUser and sp_AddNewRole. Use the ManyToMany script as your base table structures. The stored procedure should accept the parameters needed to input the data for each table. NOTE: You do not need to input the UserID or RoleID. These are surrogate keys and the system automatically inserts them when you insert a row in the tables.   On execution, the stored procedure should check the database to see if the user exists, if so,...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again. public class Product { private String name; private double price; public Product(String productName, double productPrice) { name = productName; price = productPrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double amount) {...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4 3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) {...
Write a SQL script to add another table to your database. Include these fields in your...
Write a SQL script to add another table to your database. Include these fields in your Product table: Field Name Description Data Type Sample Value ProductID Product ID integer 5 ProductName Product Name varchar(50) candle Description Product Description varchar(255) Bee’s wax candle picUrl Filename of the product’s picture varchar(50) candle.gif Price Product Price decimal 10.99           ProductID should be the Primary Key. It is an auto-increment field.           The Price field stores prices to 7 significant digits and to 2...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
1. How do indexes improve SQL query performance? 2. How can stored procedure design improve query...
1. How do indexes improve SQL query performance? 2. How can stored procedure design improve query times in a data mart? 3. Why/How does de-normalization of a data mart design improve performance of queries? 4. What is a different between Client-Servervs. Distributed Architecture? 5. What are some primary features of a 3-tier architecture design?
Write a bash script named q1f.sh that will edit the PATH environment variable to include the...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the sourcefiles directory in your home directory and make the new variable global. Please show picture of output.
Write a script named countmatches that expects at least two arguments on the command line. The...
Write a script named countmatches that expects at least two arguments on the command line. The first argument is the pathname of a dna file containing a valid DNA string with no newline characters or white space characters of any kind within it. (It will be terminated with a newline character.) This dna file contains nothing but a sequence of the bases a, c, g, and t in any order. The remaining arguments are strings containing only the bases a,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT