Simple Oracle variable SQL Assignment

后端 未结 5 521
一整个雨季
一整个雨季 2021-01-17 20:35

Despite having spent an hour researching I can\'t seem to figure out how to correctly define a variable and then use it in your SQL.

This is what I have so far produ

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-17 21:02

    To accomplish what you're attempting in Toad, you don't need to declare the variable at all. Simply include your variable prefaced with a colon and Toad will prompt you for the variable's value when you execute the query. For example:

    select * from all_tables where owner = :this_is_a_variable;
    

    If this doesn't work initially, right-click anywhere in the editor and make sure "Prompt for Substitution Variables" is checked.

    If you really want to do it similarly to the way SQL Server handles variables (or you want to be able to do the same thing in SQL*Plus), you can write it as follows:

    var this_is_a_variable varchar2(30); 
    
    exec :this_is_a_variable := 'YOUR_SCHEMA_NAME';
    
    print this_is_a_variable;
    
    select * from all_tables where owner = :this_is_a_variable;
    

    However, to make this work in Toad, you'll need to run it through "Execute as script", rather than the typical "Execute statement" command.

提交回复
热议问题