Giter VIP home page Giter VIP logo

dynamic-sql-select's Introduction

Select records from table without writing the columns names ๐ŸŽฏ

This stored procedure helps to retrieve the records from a table without writing columns names inside the stored procedure.

How it was made ๐ŸŽฅ

  • create the procedure and give it a name
  • integrate some checks to make sure the schema and table name were provided; if one of these two parameters haven't been provided the store procedure will return a message
  • start composing the cursor which will go through the system table (INFORMATION_SCHEMA.COLUMNS); it will take the columns name based on the schema and table name that we send using the stored procedure parameters
  • inside the cursor i will add each column name to the variable that will contains the whole select statement (@qry)
  • after the cursor was closed and deallocated i add the from and where clause
  • in the end i execute the @qry variable which contains the select statement

Execution syntax ๐Ÿ‘ฉโ€๐Ÿ’ป

exec SP_Dynamic_Select @schema_name = 'your schema name', @table='your table name', @column_n = 'your column name', @value = 'your value'(based on this you'll retrieve specific records)

Update ๐Ÿ“ข

Changes:
- added the error handling part
- integrated binding params to protect the db against SQL Injection
- used the trim function to ensure data consistency and correctness & quotename for replacing square brackets and beautifying the code

!! This stored procedure can only be used if you want to retrieve all the columns in a table !! โ€ผ For those who want to run this sp on older versions of SQL Server, replace the trim function with a combination of ltrim and rtrim functions (example: ltrim(rtrim(column_name)) ); If you have permission, update the db compatibility level to 130 and you can forget about the above combination ๐Ÿ˜‰

!! This stored procedure can only be used if you want to retrieve all the columns in a table !!

dynamic-sql-select's People

Contributors

elenad25 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

gtechsltn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.